package o2o.web.actions.pay;


// 订单分成

import com.google.gson.Gson;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.Geohash;
import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import map.HttpRequest;
import o2o.cache.agent.*;
import o2o.cache.order.*;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopManagerRelationMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.AgentManagerInfo;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.device.door.DeviceCompanyContact;
import o2o.dao.PO.divide.DIVIDING_CODE;
import o2o.dao.PO.divide.DividingInfo;
import o2o.dao.PO.game.BOOS_TYPE;
import o2o.dao.PO.game.GAME_TYPE;
import o2o.dao.PO.order.*;
import o2o.postman.model.CityPostman;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.sys.CITY_ADMIN_LEVEL_TYPE;
import o2o.dao.PO.sys.SYS_VARIABLE_PAOTUI;
import o2o.dao.PO.sys.SysCityAdmin;
import o2o.dao.PO.sys.SysSettingValueKey;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysVariablePaotuiVO;
import o2o.system.data.CITY_ADMIN_SHARE_MONEY_TYPE;
import o2o.system.data.PLAT_SHARE_CITY_ADMIN_MONEY_TYPE;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.divide.DividingMan;
import o2o.web.actions.order.OrderMan;
import o2o.web.actions.sys.SysVariablePaotuiAction;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class OrderPayDistribute {

    //普通订单分成收入
    //订单收入算法
    //1.如果所有配送费收入 是平台的
    //1.1 没有活动
    //        店铺收入 = 商品总金额*店铺分成比例
    //1.2 有店铺活动 （无平台活动）
    //       店铺收入 =商品总金额*店铺分成比例 - 店铺活动金额
    //       平台收入 = 配送费 + （商品总金额-店铺活动金额）*平台分成比例
    //1.3 有平台活动 （无店铺活动）
    //2.配送费是店铺出的
    public static ResultInfo addNormalOrderPayV2(String orderNo, String payType, String payerAppId, String payerUserAccount, String username, OrderInfo orderInfo, ShopInfo shopInfo) {
        if (null == orderInfo) {
            return new ResultInfo("分成出错，订单信息为空·");
        }
        System.out.println("订单是否分成~~~" + SYS_SETTINGS.ARRIVE_PAY_IS_DISTRIBUTE);
        System.out.println(SYS_SETTINGS.ARRIVE_PAY_IS_DISTRIBUTE == 0 && (orderInfo.getIsArrivePay() != null && orderInfo.getIsArrivePay() == 1));
        // 如果系统设置了货到付款的订单不分成的话 就直接不分成了
        if (SYS_SETTINGS.ARRIVE_PAY_IS_DISTRIBUTE == 0 && (orderInfo.getIsArrivePay() != null && orderInfo.getIsArrivePay() == 1)) {
            return new ResultInfo();
        }
        //add by zhuwx 2018-9-25
        OrderPayInfo orderPayInfoR = OrderPayMemory.getInstance().get(orderNo);
        if (null != orderPayInfoR) {
            return new ResultInfo("已经分成过了");
        }

        //商品总金额
        Double totalGoodsMoney = orderInfo.getTotalGoodsMoney();//商品的金额
        //支付金额
        Double payMoney = orderInfo.getTotalPrice();//用户支付的金额
        //配送费
        Double deliveryPrice = orderInfo.getDeliveryPrice();//配送费
        // 配送费分成  若是配送费出现减免的话 应该按照原配送费进行分成
        if (ValidateUtil.isAbsDouble(orderInfo.getOriginalYunFei()) && orderInfo.getOriginalYunFei() > 0.0d) {
            deliveryPrice = orderInfo.getOriginalYunFei();
        }

        Double packetPrice = orderInfo.getPacketPrice();//打包费

        // 税费
        Double taxMoney = orderInfo.getGoodsTaxMoney();

        if (null == taxMoney) {
            taxMoney = 0d;
        }
        if (null == payMoney) {
            payMoney = 0d;
        }
        if (null == deliveryPrice) {
            deliveryPrice = 0d;
        }
        if (null == packetPrice) {
            packetPrice = 0d;
        }
//        if (ValidateUtil.isNotAbsDouble(totalGoodsMoney)) {
//            if (null == deliveryPrice) {
//                orderInfo.setDeliveryPrice(0d);
//            }
//            if (null == packetPrice) {
//                orderInfo.setPacketPrice(0d);
//            }
//            if (ValidateUtil.isNotAbsDouble(orderInfo.getTotalMoney())) {
//                if (ValidateUtil.isAbsDouble(payMoney)) {
//                    orderInfo.setTotalMoney(JYMathDoubleUtils.add(JYMathDoubleUtils.add(payMoney, deliveryPrice), packetPrice));
//                }
//            }
//            totalGoodsMoney = JYMathDoubleUtils.sub(orderInfo.getTotalMoney(), deliveryPrice);
//        }

        Double shopRate = 0d;
        Double shopTotalMoney = totalGoodsMoney;//店铺实际订单金额 未分成前（商品总金额 加上应该加的配送费 减掉店铺减的金额）
        Double shopMoney = 0d;//店铺实际收入 分成后
        Double postmanRate = 0d;
        Double postmanMoney = 0d;
        // 所有的钱  = 商品总金额 + 打包费(可能不包含) + 税费 (目前不包含配送费) ---> 计算最小抽成金额使用
        Double disMoney = shopTotalMoney;

        //活动金额处理
        // 店铺活动金额
        Double shopAwardMoney = orderInfo.getShopAwardMoney();//活动满减金额-店铺减金额
        // 平台活动金额
        Double platformAwardMoney = orderInfo.getPlatformAwardMoney();//活动满减金额-平台减金额
        // 代理活动金额
        Double cityAdminAwardMoney = ValidateUtil.isAbsDouble(orderInfo.getCityAdminAwardMoney()) ? orderInfo.getCityAdminAwardMoney() : 0.0d;//活动满减金额-代理减金额
        // 城代活动金额
//        Double cityAdminAwardMoney = 0d;//活动满减金额-

        Long sellerId = orderInfo.getShopId();
        String sellerUsername = "";
        Double shopSharePacketPrice = 0.0d;
        // 单个商品 每个商品抽多少钱
        double plateGoodDrawMoney = 0.0d;
        double totalGoodsDrawMoney = 0.0d; // 一共抽成了多少钱
        // 单独抽每个订单的钱
        if (SYS_SETTINGS.IS_PLATE_GOODS_DRAW_MONEY == 1 && shopInfo != null && shopInfo.getPlateDrawGoodMoney() > 0.0d) {
            plateGoodDrawMoney = shopInfo.getPlateDrawGoodMoney();
            totalGoodsDrawMoney = calculateTotalGoodsDrawMoney(orderNo, shopInfo.getPlateDrawGoodMoney());
        }
        // 分享者金额
        String shareUsername = orderInfo.getShareUsername();
        Double sharerShareRate = 0.0d;
        double sharerShareMoney = 0.0d;
        // 分享者分成比例
        if (SYS_SETTINGS.HAS_GOODS_DISTRIBUTE == 1 && ValidateUtil.isNotNull(shareUsername) && orderInfo.getOrderType() - ORDER_TYPE.GROUP_ORDER == 0) {
            // if (shopInfo != null && ValidateUtil.isAbsDouble(shopInfo.getSharerShareRate())) {
            //     sharerShareRate = JYMathDoubleUtils.div(shopInfo.getSharerShareRate(), 100);
            //     sharerShareRate = JYMathDoubleUtils.scale(sharerShareRate);
            //     sharerShareMoney = JYMathDoubleUtils.mul(sharerShareRate, totalGoodsMoney);
            //     sharerShareMoney = JYMathDoubleUtils.scale(sharerShareMoney);
            // }
            if (ValidateUtil.isAbsDouble(orderInfo.getSharerShareMoney())) {
                sharerShareMoney = orderInfo.getSharerShareMoney();
            }
        }

        // -----------1. 店铺分成
        // 多级代理参与分成时，店铺分成有变化
        if (null != SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY && 0 == CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN_MANAGE_DELIVERY - SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY) {
            // 例如，商家D，完成了一单
            // 商品金额：35
            // 餐盒费：5
            // 配送费：3
            //
            // 商家活动：-1（满20-1）
            // 代理活动（松江）：-4（满30-4）
            // 平台活动：-2（满30-2）
            //
            // 用户实际支付：35+5+3-1-4-2 = 36
            //
            // 商家收入：                  （35）*80% -1 + 5 = 32
            // 平台收入：	                （35）*4% -2  = -0.6
            // 代理收入（安图）：（35）*3% = 1.05
            // 代理收入（松江）： 36-32-(-0.6)-1.05 = 3.55
            if (null != orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.OPEN_VIP_ORDER - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.HUO_YUN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.WALLET_IN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.WALLET_DEPOSIT_IN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) { //跑腿订单
                if (null != shopInfo) {
                    sellerId = shopInfo.getId();
                    sellerUsername = shopInfo.getOwnUsername();
                    // 自取的外卖订单 可能会有单独的抽成比例设置
                    if (Optional.ofNullable(SYS_SETTINGS.HAS_SHOP_ZIQU_SHARE).orElse(0) - 1 == 0
                            && (orderInfo.getOrderType() - ORDER_TYPE.SHOP_GOODS == 0 && orderInfo.getIsZiQu() - 1 == 0)) {
                        shopRate = shopInfo.getZiQuShareRate();// 自取订单分成比例
                    } else {
                        shopRate = shopInfo.getMoneyRate();//分配比例
                    }
                    // shopRate = shopInfo.getMoneyRate();//分配比例
                }

                if (null == SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY) {
                    System.out.println("未设置店铺是否分成配送费部分");
                    return new ResultInfo("操作失败，请稍后再试。1");
                }

                //店铺是自配送
                if (null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
                    // 待计算金额
                    disMoney = JYMathDoubleUtils.add(disMoney, deliveryPrice);
                    Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成 1 参与
                    if (0 == 1 - isDeliveryShare) { //店铺分成时  配送费参与分成
                        //配送费店铺参与分成
                        shopTotalMoney = JYMathDoubleUtils.add(totalGoodsMoney, deliveryPrice);//订单总金额+配送费
                    }
                }

                if (ValidateUtil.isAbsDouble(shopRate)) { //单独设置了店铺分成比例

                } else { //没有单独设置  按平台比例11111
                    if (0 == 1 - orderInfo.getIsZiQu()) { //自取订单  无配送费
//                    shopRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.SHOP_RATE_DAODIAN).getVal() + "");
                        if (null == SYS_SETTINGS.SHOP_RATE_DAODIAN) {
                            System.out.println("未设置到店消费类店铺分成比例");
                            return new ResultInfo("操作失败，请稍后再试。2");
                        }
                        shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_DAODIAN + "");
                    } else { //配送类订单
                        if (null == SYS_SETTINGS.SHOP_RATE_PEISONG) {
                            System.out.println("未设置配送类店铺分成比例");
                            return new ResultInfo("操作失败，请稍后再试。3");
                        }
                        shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_PEISONG + "");
                    }
                }

//                // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
//                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 1 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                    // 1 同店铺分成比例
//                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, packetPrice);//打包费参与分成
//                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 2 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                    // 2 按城市统一设置打包费
//                    DividingInfo dividingInfoByCity = DividingMan.getDividingInfoByCity(orderInfo.getCity());
//                    if (null == dividingInfoByCity) {
//                        return new ResultInfo("城市未设置打包费分成");
//                    }
//                    Double shopPacketRate = dividingInfoByCity.getShopPacketRate();
//                    if (ValidateUtil.isNotAbsDouble(shopPacketRate)) {
//                        shopPacketRate = 0D;
//                    }
//                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(shopPacketRate, 100)));//打包费参与分成
//                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) { // 按照对店铺的打包费分成设置
//                    // 按照对店铺的打包费分成设置
//                }

                // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 1 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    // 1 同店铺分成比例
                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, packetPrice);//打包费参与分成
                }
                // 打包费 - 待计算金额
                disMoney = JYMathDoubleUtils.add(disMoney, packetPrice);
                // 税
                if (null == SYS_SETTINGS.IS_SHARE_TAX_MONEY) {
                    System.out.println("平台未设置税费是否分成");
                }

                if (null != SYS_SETTINGS.IS_SHARE_TAX_MONEY && 0 == 1 - SYS_SETTINGS.IS_SHARE_TAX_MONEY
                        && taxMoney != null && taxMoney > 0) { // 有税费参与分成
                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, taxMoney);
                }
                // 税费 - 待计算金额
                if (taxMoney != null && taxMoney > 0) {
                    disMoney = JYMathDoubleUtils.add(disMoney, taxMoney);
                }
                if (SYS_SETTINGS.SHOP_CHOU_YONG_MODEL != null && SYS_SETTINGS.SHOP_CHOU_YONG_MODEL == 1) {
                    shopMoney = calculateShopMoneyByFixedChouYong(orderInfo.getShopId(), shopTotalMoney, orderInfo.getAgentId());
                    // 如果没有设置的话 则会返程-1.0d,
                    if (shopMoney <= 0.0d) {
                        shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100)); // 分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
                    } else {
                        shopRate = 0.0d;
                    }
                } else {
                    shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100)); // 分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
                }
//                shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100));//分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了


//                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                if (null == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY || 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    shopMoney = JYMathDoubleUtils.add(shopMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 2 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    // 2 按城市统一设置打包费
                    DividingInfo dividingInfoByCity = DividingMan.getShopPacketDividingInfoByCity(orderInfo.getCity());
                    if (null == dividingInfoByCity) {
                        return new ResultInfo("城市未设置打包费分成");
                    }
                    Double shopPacketRate = dividingInfoByCity.getShopPacketRate();
                    if (ValidateUtil.isNotAbsDouble(shopPacketRate)) {
                        shopPacketRate = 0D;
                    }
                    shopMoney = JYMathDoubleUtils.add(shopMoney, JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(shopPacketRate, 100)));//打包费参与分成
                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) { // 按照店铺设置进行分成打包费
                    shopSharePacketPrice = JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(JYMathDoubleUtils.sub(100, shopInfo.getPlateSharePacketRate()), 100));
                    shopMoney = JYMathDoubleUtils.add(shopMoney, shopSharePacketPrice);
                }


                if (null != SYS_SETTINGS.IS_SHARE_TAX_MONEY && 0 == SYS_SETTINGS.IS_SHARE_TAX_MONEY
                        && taxMoney != null && taxMoney > 0) { // 有税费不参与分成 店铺所有
                    shopMoney = JYMathDoubleUtils.add(shopMoney, taxMoney);
                }

                // 自配送订单
                //店铺是自配送
                if (orderInfo.getIsZiQu() == 0 && null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
                    Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成 1 参与
                    if (0 == 2 - isDeliveryShare) { // 店铺分成时  0 店铺不参与分成 1 配送费参与分成 按照比例设置  2 全部给商家
                        //
                        shopMoney = JYMathDoubleUtils.add(shopMoney, deliveryPrice);//订单总金额+配送费
                    }
                }

                // 以下都是减去的金额
                // 单个商品分成抽成
                if (SYS_SETTINGS.IS_PLATE_GOODS_DRAW_MONEY == 1) {
                    // 店铺分成的钱  减去总商品抽成的钱
                    if (shopMoney < totalGoodsDrawMoney) {
                        System.out.println("订单号：" + orderNo + "抽成单个商品后店铺分成为负值");
                    } else {
                        //
                        shopMoney = JYMathDoubleUtils.sub(shopMoney, totalGoodsDrawMoney);
                    }
                }
                // 在这里进行计算 - 最小抽成金额
                if (SYS_SETTINGS.HAS_MIN_SHOP_MONEY == 1) {
                    Double minExtractMoney = 0.0d;
                    if (shopInfo != null) {
                        minExtractMoney = shopInfo.getMinCharge();
                    }
                    if (ValidateUtil.isNotAbsDouble(minExtractMoney)) {
                        CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
                        if (cityAgent != null) {
                            minExtractMoney = cityAgent.getMinCharge();
                        }
                    }
                    if (ValidateUtil.isNotAbsDouble(minExtractMoney)) {
                        minExtractMoney = ValidateUtil.isAbsDouble(SYS_SETTINGS.MIN_WITHDRAWAL_AMOUNT) ? SYS_SETTINGS.MIN_WITHDRAWAL_AMOUNT : 0.0D;
                    }
                    if (JYMathDoubleUtils.sub(disMoney, shopMoney) < minExtractMoney) {
                        shopMoney = JYMathDoubleUtils.sub(disMoney, minExtractMoney);
                    }
                }
                // 如果店铺有奖励金
                if (shopAwardMoney > 0) {
                    shopMoney = JYMathDoubleUtils.sub(shopMoney, shopAwardMoney);//订单所有的钱 减去店铺奖励的金额
                }
                //配送费减免的部分不参与分成 在店铺得到的总金额中减去
                if (0 == orderInfo.getIsZiQu()) {//配送订单 且平台配送的
                    if (null != shopInfo && (null == shopInfo.getIsSelfPost() || 0 == shopInfo.getIsSelfPost())) {
                        //平台配送
                        //店铺金额减掉承担的配送费免减
                        if (ValidateUtil.isAbsDouble(orderInfo.getOriginalYunFeiShop())) {
                            shopMoney = JYMathDoubleUtils.sub(shopMoney, orderInfo.getOriginalYunFeiShop());//店铺总金额-店铺减免的配送费
                        }
                    }
                }

                // 减去分享者的优惠
                if (ValidateUtil.isAbsDouble(sharerShareMoney)) {
                    shopMoney = JYMathDoubleUtils.sub(shopMoney, sharerShareMoney); // 商家金额 减去承担的金额
                }
            } else if (null != orderInfo.getOrderType()
                    && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {
                shopTotalMoney = 0d;
            }
        } else {
            if (null != orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.OPEN_VIP_ORDER - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.WALLET_IN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.WALLET_DEPOSIT_IN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.HUO_YUN - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                    && 0 != ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) { //跑腿订单
                if (null != shopInfo) {
                    sellerId = shopInfo.getId();
                    sellerUsername = shopInfo.getOwnUsername();
                    // 自取的外卖订单 可能会有单独的抽成比例设置
                    if (Optional.ofNullable(SYS_SETTINGS.HAS_SHOP_ZIQU_SHARE).orElse(0) - 1 == 0
                            && (orderInfo.getOrderType() - ORDER_TYPE.SHOP_GOODS == 0 && orderInfo.getIsZiQu() - 1 == 0)) {
                        shopRate = shopInfo.getZiQuShareRate();// 自取订单分成比例
                    } else {
                        shopRate = shopInfo.getMoneyRate();//分配比例
                    }
                    // shopRate = shopInfo.getMoneyRate();//分配比例
                }

                if (null == SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY) {
                    System.out.println("未设置店铺是否分成配送费部分");
                    return new ResultInfo("操作失败，请稍后再试。1");
                }

                //店铺是自配送
                if (null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
                    disMoney = JYMathDoubleUtils.add(disMoney, deliveryPrice);
                    Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成 1 参与
                    if (0 == 1 - isDeliveryShare) { //店铺分成时  配送费参与分成
                        //配送费店铺参与分成
                        shopTotalMoney = JYMathDoubleUtils.add(totalGoodsMoney, deliveryPrice);//订单总金额+配送费
                    }
                }

                //如果店铺有奖励金
                if (shopAwardMoney > 0) {
                    shopTotalMoney = JYMathDoubleUtils.sub(shopTotalMoney, shopAwardMoney);//订单所有的钱 减去店铺奖励的金额
                    disMoney = JYMathDoubleUtils.sub(disMoney, shopAwardMoney);
                }
                // todo: 在这里认为他是一种营销模式 故在此模式下  大家共同承担此费用
                if (sharerShareMoney > 0) {
                    shopTotalMoney = JYMathDoubleUtils.sub(shopTotalMoney, sharerShareMoney);//订单所有的钱 分享者的钱
                    disMoney = JYMathDoubleUtils.sub(disMoney, sharerShareMoney);
                }

                if (ValidateUtil.isAbsDouble(shopRate)) { //单独设置了店铺分成比例

                } else { //没有单独设置  按平台比例11111
                    if (0 == 1 - orderInfo.getIsZiQu()) { //自取订单  无配送费
//                    shopRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.SHOP_RATE_DAODIAN).getVal() + "");
                        if (null == SYS_SETTINGS.SHOP_RATE_DAODIAN) {
                            System.out.println("未设置到店消费类店铺分成比例");
                            return new ResultInfo("操作失败，请稍后再试。2");
                        }
                        shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_DAODIAN + "");
                    } else { //配送类订单
//                    shopRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.SHOP_RATE_PEISONG).getVal() + "");

                        if (null == SYS_SETTINGS.SHOP_RATE_PEISONG) {
                            System.out.println("未设置配送类店铺分成比例");
                            return new ResultInfo("操作失败，请稍后再试。3");
                        }
                        shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_PEISONG + "");
                    }
                }

//                // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
//                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 1 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                    // 1 同店铺分成比例
//                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, packetPrice);//打包费参与分成
//                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 2 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                    // 2 按城市统一设置打包费
//                    DividingInfo dividingInfoByCity = DividingMan.getDividingInfoByCity(orderInfo.getCity());
//                    if (null == dividingInfoByCity) {
//                        return new ResultInfo("城市未设置打包费分成");
//                    }
//                    Double shopPacketRate = dividingInfoByCity.getShopPacketRate();
//                    if (ValidateUtil.isNotAbsDouble(shopPacketRate)) {
//                        shopPacketRate = 0D;
//                    }
//                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(shopPacketRate, 100)));//打包费参与分成
//                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) { // 按照店铺设置的打包费分成比例分成
//                    // 3 按照对店铺的设置进行分成打打包费
//                }

                // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 1 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    // 1 同店铺分成比例
                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, packetPrice);//打包费参与分成
                }
                // 打包费 - 待计算金额
                disMoney = JYMathDoubleUtils.add(disMoney, packetPrice);
                // 税
                if (null == SYS_SETTINGS.IS_SHARE_TAX_MONEY) {
                    System.out.println("平台未设置税费是否分成");
                }

                if (null != SYS_SETTINGS.IS_SHARE_TAX_MONEY && 0 == 1 - SYS_SETTINGS.IS_SHARE_TAX_MONEY
                        && taxMoney != null && taxMoney > 0) { // 有税费参与分成
                    shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, taxMoney);
                }
                // 税费 - 待计算金额
                if (taxMoney != null && taxMoney > 0) {
                    disMoney = JYMathDoubleUtils.add(disMoney, taxMoney);
                }
                if (SYS_SETTINGS.SHOP_CHOU_YONG_MODEL != null && SYS_SETTINGS.SHOP_CHOU_YONG_MODEL == 1) {
                    shopMoney = calculateShopMoneyByFixedChouYong(orderInfo.getShopId(), shopTotalMoney, orderInfo.getAgentId());
                    // 如果没有设置的话 则会返程-1.0d,
                    if (shopMoney <= 0.0d) {
                        shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100)); // 分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
                    } else {
                        shopRate = 0.0d;
                    }
                } else {
                    shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100)); // 分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
                }
//                shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100));//分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了

                //配送费减免的部分不参与分成 在店铺得到的总金额中减去
                if (0 == orderInfo.getIsZiQu()) {//配送订单 且平台配送的
                    if (null != shopInfo && (null == shopInfo.getIsSelfPost() || 0 == shopInfo.getIsSelfPost())) {
                        //平台配送
                        //店铺金额减掉承担的配送费免减
                        if (ValidateUtil.isAbsDouble(orderInfo.getOriginalYunFeiShop())) {
                            shopMoney = JYMathDoubleUtils.sub(shopMoney, orderInfo.getOriginalYunFeiShop());//店铺总金额-店铺减免的配送费
                        }
                    }
                }

                // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
                if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    // 0不参与(都归店铺所有)
                    shopMoney = JYMathDoubleUtils.add(shopMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 2 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                    // 2 按城市统一设置打包费
                    DividingInfo dividingInfoByCity = DividingMan.getShopPacketDividingInfoByCity(orderInfo.getCity());
                    if (null == dividingInfoByCity) {
                        return new ResultInfo("城市未设置打包费分成");
                    }
                    Double shopPacketRate = dividingInfoByCity.getShopPacketRate();
                    if (ValidateUtil.isNotAbsDouble(shopPacketRate)) {
                        shopPacketRate = 0D;
                    }
                    shopMoney = JYMathDoubleUtils.add(shopMoney, JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(shopPacketRate, 100)));//打包费参与分成
                } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {  // 按照店铺设置进行分成打包费
                    // 3 按照对店铺的设置进行分成打打包费
                    shopSharePacketPrice = JYMathDoubleUtils.mul(packetPrice, JYMathDoubleUtils.div(JYMathDoubleUtils.sub(100, shopInfo.getPlateSharePacketRate()), 100));
                    shopMoney = JYMathDoubleUtils.add(shopMoney, shopSharePacketPrice);
                }

                if (null != SYS_SETTINGS.IS_SHARE_TAX_MONEY && 0 == SYS_SETTINGS.IS_SHARE_TAX_MONEY
                        && taxMoney != null && taxMoney > 0) { // 有税费不参与分成 店铺所有
                    shopMoney = JYMathDoubleUtils.add(shopMoney, taxMoney);
                }

                // 单个商品分成抽成
                if (SYS_SETTINGS.IS_PLATE_GOODS_DRAW_MONEY == 1) {
                    // 店铺分成的钱  减去总商品抽成的钱
                    if (shopMoney < totalGoodsDrawMoney) {
                        System.out.println("订单号：" + orderNo + "抽成单个商品后店铺分成为负值");
                    } else {
                        //
                        shopMoney = JYMathDoubleUtils.sub(shopMoney, totalGoodsDrawMoney);
                    }
                }
                // 自配送订单
                //店铺是自配送
                if (orderInfo.getIsZiQu() == 0 && null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
                    Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成 1 参与
                    if (0 == 2 - isDeliveryShare) { // 店铺分成时  0 店铺不参与分成 1 配送费参与分成 按照比例设置  2 全部给商家
                        //
                        shopMoney = JYMathDoubleUtils.add(shopMoney, deliveryPrice);//订单总金额+配送费
                    }
                }

                // 在这里进行计算 - 最小抽成金额
                if (SYS_SETTINGS.HAS_MIN_SHOP_MONEY == 1) {
                    Double minExtractMoney = 0.0d;
                    if (shopInfo != null) {
                        minExtractMoney = shopInfo.getMinCharge();
                    }
                    if (ValidateUtil.isNotAbsDouble(minExtractMoney)) {
                        CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
                        if (cityAgent != null) {
                            minExtractMoney = cityAgent.getMinCharge();
                        }
                    }
                    if (ValidateUtil.isNotAbsDouble(minExtractMoney)) {
                        minExtractMoney = ValidateUtil.isAbsDouble(SYS_SETTINGS.MIN_WITHDRAWAL_AMOUNT) ? SYS_SETTINGS.MIN_WITHDRAWAL_AMOUNT : 0.0D;
                    }
                    if (JYMathDoubleUtils.sub(disMoney, shopMoney) < minExtractMoney) {
                        shopMoney = JYMathDoubleUtils.sub(disMoney, minExtractMoney);
                    }
                }

            } else if (null != orderInfo.getOrderType()
                    && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {
                shopTotalMoney = 0d;
            }
        }


        //2. 配送员部分
        if (0 == orderInfo.getIsZiQu() && orderInfo.getOrderType() != ORDER_TYPE.GROUP_ORDER) { //配送的订单才进来
            if (null == SYS_SETTINGS.POSTMAN_SHARE_MONEY) {
                System.out.println("未设置配送员是否参与分成");
                return new ResultInfo("操作失败，请稍后再试。4");
            }

            //如果店铺是自配送的  则无需再处理配送费部分
            if ((null != shopInfo && null != shopInfo.getIsSelfPost() && 0 != 1 - shopInfo.getIsSelfPost() && !OrderMan.isMultipleParentOrder(orderInfo))//平台配送
                    ||
                    (null != orderInfo.getOrderType()
                            && (0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType()))//跑腿代购帮办
            ) {

                Integer postManShare = SYS_SETTINGS.POSTMAN_SHARE_MONEY; //配送员分成类型（0不参与，1总金额，2参与配送费部分）
                switch (postManShare) {
                    case SysSettingValueKey.POSTMAN_SHARE_NONE: {//不参与分成
                        postmanRate = 0d;
                    }
                    break;
                    case SysSettingValueKey.POSTMAN_SHARE_ALL: {//参与订单总金额分成
                        Double totalMoney = orderInfo.getTotalMoney();//订单总金额
                        if (ValidateUtil.isNotAbsDouble(postmanRate)) {
                            //配送员参与分成比例
                            if (null == SYS_SETTINGS.POSTMAN_RATE) {
                                System.out.println("未设置配送员分成比例");
                                return new ResultInfo("操作失败，请稍后再试。5");
                            }
                            postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                        }
//                        配送员参与分成比例
//                        if (null == SYS_SETTINGS.POSTMAN_RATE) {
//                            System.out.println("未设置配送员分成比例");
//                            return new ResultInfo("操作失败，请稍后再试。5");
//                        }
//                        postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                        if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
                            CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
                            if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
                                postmanRate = cityPostman.getMoneyRate();
                            }
                        }

                        if (ValidateUtil.isAbsDouble(postmanRate)) {
                            postmanMoney = JYMathDoubleUtils.mul(totalMoney, JYMathDoubleUtils.div(postmanRate, 100));
                        }
                    }
                    break;
                    case SysSettingValueKey.POSTMAN_SHARE_DELIVERY: { //参与配送费部分
                        if (ValidateUtil.isNotAbsDouble(postmanRate)) {
                            if (null == SYS_SETTINGS.POSTMAN_RATE) {
                                System.out.println("未设置配送员分成比例");
                                return new ResultInfo("操作失败，请稍后再试。5");
                            }
                            postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                        }
//                        if (null == SYS_SETTINGS.POSTMAN_RATE) {
//                            System.out.println("未设置配送员分成比例");
//                            return new ResultInfo("操作失败，请稍后再试。5");
//                        }
//                        postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                        if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
                            CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
                            if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
                                postmanRate = cityPostman.getMoneyRate();
                            }
                        }

                        if (ValidateUtil.isAbsDouble(postmanRate)) {
                            postmanMoney = JYMathDoubleUtils.mul(deliveryPrice, JYMathDoubleUtils.div(postmanRate, 100));
                        }
                    }
                    break;
                    case SysSettingValueKey.POSTMAN_SHARE_BY_AGENT: {//3按商圈设置的配送员工资结算公式计算
                        //获取订单所在商圈
                        if (ValidateUtil.isID(orderInfo.getAgentId())) {
                            CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
                            //获取商圈配送员费用结算公式
                            if (null == cityAgent) {
                                System.out.println("订单商圈不存在。无法完成配送员分成：" + orderNo);
                                return new ResultInfo("[分成 3]订单经纬度为空，无法计算距离。");
                            }

                            if (ValidateUtil.isNotLatLng(orderInfo.getLat(), orderInfo.getLng())) {
                                System.out.println("订单经纬度为空，无法计算距离：" + orderNo);
                                return new ResultInfo("[分成 5]订单经纬度为空，无法计算距离。");
                            }

                            // 跑腿订单 3公里就近购买时，没有店铺的经纬度
                            Double km = null;
                            if (null != orderInfo.getOrderType() && 0 == orderInfo.getOrderType() - ORDER_TYPE.DAI_GOU) {
                                // 获取该商圈里的代购基础距离
                                SysVariablePaotuiVO sysVariablePaotui = SysVariablePaotuiAction.infoOfAgentAndCode(cityAgent.getId(), SYS_VARIABLE_PAOTUI.DAI_GOU);
                                if (null != sysVariablePaotui && null != sysVariablePaotui.getRang()) {
                                    try {
                                        km = Double.parseDouble(sysVariablePaotui.getRang());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        km = 3d;
                                    }
                                }
                            } else {
                                if (ValidateUtil.isNotLatLng(orderInfo.getShopLat(), orderInfo.getShopLng())) {
                                    System.out.println("订单商家经纬度为空，无法计算距离：" + orderNo);
                                    return new ResultInfo("[分成 6]操作失败，请稍后再试。");
                                }
                                //配送距离
                                Double distance = Geohash.distance(orderInfo.getLat(), orderInfo.getLng(), orderInfo.getShopLat(), orderInfo.getShopLng());//根据两点的经纬度计算距离
                                km = JYMathDoubleUtils.div(distance, 1000);//距离
                            }

                            //withinDistance 范围
                            Double psWithinDistance = cityAgent.getPsWithinDistance();
                            if (null == psWithinDistance) {
                                psWithinDistance = 0d;
                            }
                            //fixedCost 范围内价格
                            Double psFixedCost = cityAgent.getPsFixedCost();
                            if (null == psFixedCost) {
                                psFixedCost = 0d;
                            }
                            //oneKmCost 超出后每公里价格
                            Double psOneKmCost = cityAgent.getPsOneKmCost();
                            if (null == psOneKmCost) {
                                psOneKmCost = 0d;
                            }
                            //骑手工资-最大金额数（-1为不限）
                            Double psMaxMoney = cityAgent.getPsMaxMoney();
                            if (null == psMaxMoney) {
                                psMaxMoney = -1d;
                            }
                            if (null == km) {
                                km = 0d;
                            }
                            if (JYMathDoubleUtils.sub(km, psWithinDistance) <= 0) {
                                postmanMoney = psWithinDistance;
                            } else {
                                Double after = JYMathDoubleUtils.sub(km, psWithinDistance);
                                Double round = Math.ceil(after);
                                postmanMoney = JYMathDoubleUtils.add(psFixedCost, JYMathDoubleUtils.mul(round, psOneKmCost));

                                if (psMaxMoney >= 0 && JYMathDoubleUtils.sub(postmanMoney, psMaxMoney) > 0) {
                                    postmanMoney = psMaxMoney;
                                }
                            }
                        } else {
                            //订单商圈不存在，无法完成配送员分成
                            System.out.println("订单商圈不存在，无法完成配送员分成：" + orderNo);
                            return new ResultInfo("订单商圈不存在，无法完成配送员分成。" + orderNo);
                        }
                    }
                    break;
                }
            }
        }


        // 小费计算
        // create by ytk   : star
        Double tipMoney = orderInfo.getTipMoney();
        if (null == tipMoney) {
            tipMoney = 0d;
        }
        if (tipMoney != null && tipMoney > 0) { // 有小费
            if (SYS_SETTINGS.SHARE_TIP_MONEY != null && SYS_SETTINGS.SHARE_TIP_MONEY > 0) {
                switch (SYS_SETTINGS.SHARE_TIP_MONEY) {
                    case 1:
                        // 店铺 all
                        shopMoney = JYMathDoubleUtils.add(shopMoney, tipMoney);
                        break;
                    case 2:
                        // 骑手 all
                        if (orderInfo.getIsZiQu() == 0  // 配送订单
                                && null != SYS_SETTINGS.POSTMAN_SHARE_MONEY
                                && SYS_SETTINGS.POSTMAN_SHARE_MONEY != SysSettingValueKey.POSTMAN_SHARE_NONE) {  //  参与 配送费分成
                            postmanMoney = JYMathDoubleUtils.add(postmanMoney, tipMoney);
                        }
                        break;
                    case 3:
                        // 店铺 some  骑手 no
                        if (null != shopRate && shopRate > 0) {
                            shopMoney = JYMathDoubleUtils.add(shopMoney, JYMathDoubleUtils.mul(tipMoney, JYMathDoubleUtils.div(shopRate, 100)));
                        }
                        break;
                    case 4:
                        // 店铺 some 骑手 some
                        if (orderInfo.getIsZiQu() == 0  // 自取
                                && (null != shopInfo && null != shopInfo.getIsSelfPost() && 0 != 1 - shopInfo.getIsSelfPost())  // 平台配送
                                && null != SYS_SETTINGS.POSTMAN_SHARE_MONEY
                                && SYS_SETTINGS.POSTMAN_SHARE_MONEY != SysSettingValueKey.POSTMAN_SHARE_NONE) {  //  参与 配送费分成
                            postmanMoney = JYMathDoubleUtils.add(postmanMoney, JYMathDoubleUtils.mul(tipMoney, JYMathDoubleUtils.div(postmanRate, 100)));
                        }
                        if (null != shopRate && shopRate > 0) {
                            shopMoney = JYMathDoubleUtils.add(shopMoney, JYMathDoubleUtils.mul(tipMoney, JYMathDoubleUtils.div(shopRate, 100)));
                        }
                        break;
                    case 5:
                        // 店铺 0  骑手 some
                        if (orderInfo.getIsZiQu() == 0  // 自取
                                && null != SYS_SETTINGS.POSTMAN_SHARE_MONEY
                                && SYS_SETTINGS.POSTMAN_SHARE_MONEY != SysSettingValueKey.POSTMAN_SHARE_NONE) {  //  参与 配送费分成
                            postmanMoney = JYMathDoubleUtils.add(postmanMoney, JYMathDoubleUtils.mul(tipMoney, JYMathDoubleUtils.div(postmanRate, 100)));
                        }
                        break;
                    case 6:
                        // 店铺 0  骑手0
                        break;
                    default:
                        break;
                }
            }
        }
        // end

        // 代购订单-骑手垫付金额
        if (null != orderInfo.getOrderType() && 0 == orderInfo.getOrderType() - ORDER_TYPE.DAI_GOU) {
            // 获取代购订单详情
            OtherOrderInfo otherOrderInfo = OtherOrderMemory.getInstance().getByOrderNo(orderNo);
            if (null != otherOrderInfo && ValidateUtil.isAbsDouble(otherOrderInfo.getGoodsPrice())) {
                if (null != SYS_SETTINGS.POSTMAN_SHARE_DAIGOU_GOODS_MONEY) {
                    if (-1 == SYS_SETTINGS.POSTMAN_SHARE_DAIGOU_GOODS_MONEY) {
                        // 全部给骑手
                        postmanMoney = JYMathDoubleUtils.add(postmanMoney, otherOrderInfo.getGoodsPrice());
                    } else {
                        // 都给平台
                    }
                }
            }
        }

        //3. 业务员分成
        String businessUsername = null; //业务员账号
        Double businessRate = 0d; //业务员分成比例
        Double businessMoney = 0d;//业务员分成金额
        //获取店铺的业务员
        if (ValidateUtil.isID(orderInfo.getShopId())) {
            String managerUsername = ShopManagerRelationMemory.getInstance().getManagerByShopId(orderInfo.getShopId());
            UserInfo managerInfo = UserInfoMemory.getInstance().getUserInfo(managerUsername);
            if (null != managerInfo && ValidateUtil.isAbsDouble(managerInfo.getRate())) {
                businessUsername = managerInfo.getUsername();
                businessRate = managerInfo.getRate();
                //分成支付金额部分
                businessMoney = JYMathDoubleUtils.mul(payMoney, JYMathDoubleUtils.div(businessRate, 100));//分配金额
            }
        }

        //3.1 区域主管
        String agentManagerUsername = null; // 区域主管账号
        Double agentManagerRate = 0d; // 区域主管分成比例
        Double agentManagerMoney = 0d;// 区域主管分成钱数
        //获取店铺的业务员
        if (ValidateUtil.isID(orderInfo.getAgentId()) && ORDER_TYPE.SHOP_GOODS == orderInfo.getOrderType() && SYS_SETTINGS.AGENT_MANAGER_IS_SHARE_MONEY == 1) {
            CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
            if (cityAgent != null && ValidateUtil.isNotNull(cityAgent.getAgentManagerUsername())) {
                AgentManagerInfo info = AgentManagerMemory.getInstance().get(cityAgent.getAgentManagerUsername());
                if (info != null) {
                    agentManagerUsername = info.getUsername();
                    agentManagerRate = cityAgent.getAgentManagerShareRate() == null ? 0.0d : cityAgent.getAgentManagerShareRate();
                    if (ValidateUtil.isNotAbsDouble(agentManagerRate)) { // 商圈上没有设置的话  直接用代理的
                        agentManagerRate = ValidateUtil.isAbsDouble(info.getShareRate()) ? info.getShareRate() : 0.0d;
                    }
                    //分成支付金额部分
                    agentManagerMoney = JYMathDoubleUtils.mul(payMoney, JYMathDoubleUtils.div(agentManagerRate, 100));//分配金额
                }
            }
        }


        // 保留两位小数
        shopMoney = JYMathDoubleUtils.scaleHalfUp(shopMoney);
        postmanMoney = JYMathDoubleUtils.scaleHalfUp(postmanMoney);
        businessMoney = JYMathDoubleUtils.scaleHalfUp(businessMoney);
        shopAwardMoney = JYMathDoubleUtils.scaleHalfUp(shopAwardMoney);
        platformAwardMoney = JYMathDoubleUtils.scaleHalfUp(platformAwardMoney);
        agentManagerMoney = JYMathDoubleUtils.scaleHalfUp(agentManagerMoney);

        Double platformRate = 0d;//平台分成比例
        Double platformMoney = 0d;//平台分成金额
        OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getUsername(), sellerUsername, sellerId, payType, 1, username, 0, DateUtils.getCurrTime(),
                JYMathDoubleUtils.div(shopRate, 100), shopMoney, JYMathDoubleUtils.div(postmanRate, 100), postmanMoney, JYMathDoubleUtils.div(platformRate, 100), platformMoney,
                shopAwardMoney, platformAwardMoney);
        orderPayInfo.setBuyerAppId(payerAppId);
        orderPayInfo.setBuyerLogonId(payerUserAccount);

        // 分享者 分成比例
        if (ValidateUtil.isNotNull(shareUsername)) {
            orderPayInfo.setShareUsername(shareUsername);           // 分享者账号
            orderPayInfo.setSharerShareRate(sharerShareRate);       // 分享者比例
            orderPayInfo.setSharerShareMoney(sharerShareMoney);     // 分享者金额
        }

        // 如果已经有骑手了
        if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
            orderPayInfo.setPostman(orderInfo.getPostManUsername());
        }
        // 如果已经有二级骑手了
        if (ValidateUtil.isNotNull(orderInfo.getSecondaryPostManUsername())) {
            orderPayInfo.setSecondaryPostman(orderInfo.getSecondaryPostManUsername());
        }

        //业务员分成
        orderPayInfo.setBusiness(businessUsername);
        orderPayInfo.setBusinessRate(JYMathDoubleUtils.div(businessRate, 100));
        orderPayInfo.setBusinessMoney(businessMoney);

        // 3.1 区域主管
        orderPayInfo.setAgentMangerUsername(agentManagerUsername);
        orderPayInfo.setAgentManagerRate(JYMathDoubleUtils.div(agentManagerRate, 100));
        orderPayInfo.setAgentManagerMoney(agentManagerMoney);

        //4. 城代、平台分成
        String cityAdminUsername = null; //城代账号
        Double cityAdminRate = 0d; //城代分成比例
        Double cityAdminMoney = 0d;//城代分成金额

        String cityAdminUsername1 = null; //城代账号
        Double cityAdminRate1 = 0d; // 直系城代分成比例
        Double cityAdminMoney1 = 0d;// 直系城代分成金额
        String cityAdminUsername2 = null; //城代账号
        Double cityAdminRate2 = 0d; // 二级城代分成比例
        Double cityAdminMoney2 = 0d;//二级城代分成金额
        String cityAdminUsername3 = null; //城代账号
        Double cityAdminRate3 = 0d; //城代分成比例
        Double cityAdminMoney3 = 0d;//城代分成金额
        String cityAdminUsername4 = null; //城代账号
        Double cityAdminRate4 = 0d; //城代分成比例
        Double cityAdminMoney4 = 0d;//城代分成金额
        if (null != SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY) {
            //0不抽取，1只有店铺和平台方式，2平台统一设置，最后金额归属平台，3店铺的直属代理是最大受益人
            switch (SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY) {
                case CITY_ADMIN_SHARE_MONEY_TYPE.NONE: {//0不抽取
                    //平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员
                    platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员
                    platformMoney = JYMathDoubleUtils.sub(platformMoney, agentManagerMoney);  // 平台收入 = 支付金额 - 分成给店铺 - 分成给配送员 - 分给业务员 - 区域主管的
                    platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);// 四舍五入
                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);

                    // 是否开启上下级代理模式，开启的话，记录代理信息，方便统计代理下的店铺金额
                    if (null != SYS_SETTINGS.IS_UPPER_LOWER_SYSADMIN && 0 == 1 - SYS_SETTINGS.IS_UPPER_LOWER_SYSADMIN) {
                        SysCityAdmin cityAdmin = null;
                        if (ValidateUtil.isID(sellerId)) {
                            // 获取店铺所属商圈
                            Long agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(sellerId);
                            if (ValidateUtil.isID(agentId)) {
                                // 获取商圈的直属代理信息
                                List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                                if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {

                                }
                            }
                        }
                        if (null != cityAdmin) {
                            // 获取此代理所属级别
                            if (null != cityAdmin.getCurrLevel()) {
                                // 直属代理分成金额
                                cityAdminUsername = cityAdmin.getUsername();// 直属代理账号
                                switch (cityAdmin.getCurrLevel()) {
                                    case 1: {// 相对于平台的第一级是直属代理
                                        // 平台抽佣金
                                        // 无级别，所有金额属于平台
                                    }
                                    break;
                                    case 2: { // 相对于平台的第2级
                                        // 平台抽佣金、一级代理抽 佣金
                                        cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    }
                                    break;
                                    case 3: {// 相对于平台的第3级  受益者：平台、第一级、第二级，第三级未直接代理
                                        cityAdminUsername1 = cityAdmin.getShareUsername1();
                                        cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    }
                                    break;
                                    case 4: {// 相对于平台的第4级
                                        cityAdminUsername1 = cityAdmin.getShareUsername1();
                                        cityAdminUsername2 = cityAdmin.getShareUsername2();
                                        cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    }
                                    break;
                                    case 5: {// 相对于平台的第5级
                                        cityAdminUsername1 = cityAdmin.getShareUsername1();
                                        cityAdminUsername2 = cityAdmin.getShareUsername2();
                                        cityAdminUsername3 = cityAdmin.getShareUsername3();
                                        cityAdminUsername4 = cityAdmin.getShareUsername4();
                                    }
                                    break;
                                }


                                // 直接城代分成
                                orderPayInfo.setCityAdmin(cityAdminUsername);
                                orderPayInfo.setCityAdminRate(0d);
                                orderPayInfo.setCityAdminMoney(0d);
                                orderPayInfo.setCityAdminAward(0d);

                                // 相对平台的第一级，即省级代理分成
                                orderPayInfo.setCityAdmin1(cityAdminUsername1);
                                orderPayInfo.setCityAdminRate1(0d);
                                orderPayInfo.setCityAdminMoney1(0d);
                                orderPayInfo.setCityAdminAward1(0d);

                                // 相对平台的第二级，即市级代理分成
                                orderPayInfo.setCityAdmin2(cityAdminUsername2);
                                orderPayInfo.setCityAdminRate2(0d);
                                orderPayInfo.setCityAdminMoney2(0d);
                                orderPayInfo.setCityAdminAward2(0d);

                                // 相对平台的第三级，即区级代理分成
                                orderPayInfo.setCityAdmin3(cityAdminUsername3);
                                orderPayInfo.setCityAdminRate3(0d);
                                orderPayInfo.setCityAdminMoney3(0d);
                                orderPayInfo.setCityAdminAward3(0d);

                                // 相对平台的第四级，即乡镇级代理分成
                                orderPayInfo.setCityAdmin4(cityAdminUsername4);
                                orderPayInfo.setCityAdminRate4(0d);
                                orderPayInfo.setCityAdminMoney4(0d);
                                orderPayInfo.setCityAdminAward4(0d);
                            }
                        }
                    }
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN: { //1只有店铺和平台方式
                    ResultInfo resultInfo = getPlateShareCityAdminRate(orderInfo.getAgentId(), orderInfo.getShopId(), orderInfo.getShopCity());//平台抽取代理分成比例
                    if (null != resultInfo && resultInfo.isSuccess()) {
                        platformRate = (Double) resultInfo.getObj();
                        if (ValidateUtil.isNotAbsDouble(platformRate)) {
                            platformRate = 100d;
                        }
                        cityAdminUsername = resultInfo.getMessage();
                    } else {
                        platformRate = 100d;
                    }

                    cityAdminRate = JYMathDoubleUtils.sub(100, platformRate);
                    switch (SYS_SETTINGS.PLAT_SHARE_CITY_ADMIN_MONEY_TYPE) {//平台抽取城代方式
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.NONE: {//不抽取
//                    cityAdminRate = 1d;//平台不抽取的话，所有的盈利都是代理的，这个1也不准确
                            platformRate = 0d;
                            cityAdminRate = 0d;
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员(-活动减金额-代理活动金额) ?? 为啥要减去优惠的金额?
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformAwardMoney);
//                             减去代理活动金额
//                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminAwardMoney);
                            // 减去区域主管的
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.TOTAL_MONEY: {//抽取订单总金额部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getTotalMoney(), JYMathDoubleUtils.div(platformRate, 100));
                            platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                            // 减去区域主管的
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.DELIVER_MONEY: {//抽取配送费部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getDeliveryPrice(), JYMathDoubleUtils.div(platformRate, 100));
                            platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                            // 减去区域主管的
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.PAY_MONEY: {//抽取支付金额部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), JYMathDoubleUtils.div(platformRate, 100));
                            platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);// 四舍五入
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                            // 减去区域主管的
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);
                        }
                        break;
                    }

                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);

                    //城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.PLATFORM_MANAGE: { //2平台统一设置，最后剩余的金额归属平台
//                    System.out.println("城市代理参与分成");
                    Long pId = null;
//            sellerId // 店铺id
                    Double shareMoney = totalGoodsMoney;
                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        Double plateSharePrice = JYMathDoubleUtils.sub(packetPrice, shopSharePacketPrice);
                        if (plateSharePrice < 0.0d) {
                            plateSharePrice = 0.0d;
                        }
                        shareMoney = JYMathDoubleUtils.add(shareMoney, plateSharePrice);// 店铺分完打包费后 剩余的部分分 由平台以及相关的在此分成
                    } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                    }
                    // 减去店铺活动的钱
                    shareMoney = JYMathDoubleUtils.sub(shareMoney, shopAwardMoney);
                    // 减去代理活动的钱
                    shareMoney = JYMathDoubleUtils.sub(shareMoney, cityAdminAwardMoney);
//             目前先减去活动的钱(目前这样算)
//            shareMoney = JYMathDoubleUtils.div(shareMoney, platformAwardMoney);
                    // 由城市获取商圈
                    // 获取店铺属于的商圈
                    Long agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(sellerId);
                    List<String> userAdminList = null;
                    if (ValidateUtil.isID(agentId)) {
                        userAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                    }
                    // 由商圈获取所属的 管理者
                    List<SysCityAdmin> sysCityAdminList = null;
                    if (ValidateUtil.isAbsList(userAdminList)) {
                        String useradmin = userAdminList.get(0);
                    }
                    // 所述的
                    if (ValidateUtil.isAbsList(sysCityAdminList)) {
                        pId = sysCityAdminList.get(0).getId();
                    }

//            CityAdminShopRelation byShopId = CityAdminShopRelationMemory.getInstance().getByShopId(sellerId);
                    if (pId != null) {
                        // 一级
                        SysCityAdmin byId = null;
                        if (byId != null) {
                            pId = byId.getPId();
//                    if (ValidateUtil.isAbsDouble(byId.getShareRate1()) && byId.getShareRate1() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                            if (null != byId.getShareRate1() && byId.getShareRate1() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                cityAdminUsername1 = byId.getUsername();
                                cityAdminRate1 = JYMathDoubleUtils.div(byId.getShareRate1(), 100);
                                if (null != cityAdminRate1 && cityAdminRate1 >= 0) {
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                }
                            }
                        } else {
                            pId = null;
                        }
                        // 二级代理的
                        if (ValidateUtil.isID(pId)) {

                            if (byId != null) {
                                pId = byId.getPId();
//                        if (ValidateUtil.isAbsDouble(byId.getShareRate2()) && byId.getShareRate2() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                if (byId.getShareRate2() != null && byId.getShareRate2() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername2 = byId.getUsername();
                                    cityAdminRate2 = JYMathDoubleUtils.div(byId.getShareRate2(), 100);
                                    if (cityAdminRate2 != null && cityAdminRate2 >= 0) {
                                        cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        // 三级代理的
                        if (ValidateUtil.isID(pId)) {

                            if (byId != null) {
                                pId = byId.getPId();
//                        if (ValidateUtil.isAbsDouble(byId.getShareRate3()) && byId.getShareRate3() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                if (null != byId.getShareRate3() && byId.getShareRate3() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername3 = byId.getUsername();
                                    cityAdminRate3 = JYMathDoubleUtils.div(byId.getShareRate3(), 100);
//                            if (ValidateUtil.isAbsDouble(cityAdminRate3) && cityAdminRate3 > 0) {
                                    if (null != cityAdminRate3 && cityAdminRate3 >= 0) {
                                        cityAdminMoney3 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate3);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        if (ValidateUtil.isID(pId)) {
                            if (byId != null) {
                                pId = byId.getPId();
                                if (null != byId.getShareRate4() && byId.getShareRate4() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername4 = byId.getUsername();
                                    cityAdminRate4 = JYMathDoubleUtils.div(byId.getShareRate4(), 100);
//                            if (ValidateUtil.isAbsDouble(cityAdminRate4) && cityAdminRate4 > 0) {
                                    if (null != cityAdminRate4 && cityAdminRate4 >= 0) {
                                        cityAdminMoney4 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate4);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        // 先减去 店铺的
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);
                        cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                        cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                        cityAdminMoney3 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney3);// 四舍五入
                        cityAdminMoney4 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney4);// 四舍五入
                        // 在减去代理的
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(platformMoney, cityAdminMoney1), cityAdminMoney2), cityAdminMoney3), cityAdminMoney4);
                        // 减去区域主管的
                        platformMoney = JYMathDoubleUtils.sub(platformMoney, agentManagerMoney);
                    }

                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);

                    //城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                    // 直接城市级联分成 （镇代理）
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);

                    // 二级城市代理 （县代理）
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);

                    // 三级城市代理 （市代理）
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);

                    // 四级城市代理 （省代理）
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN_MANAGE: { //3店铺的直属代理是最大受益人
                    // 例如，商家D，完成了一单
                    // 商品金额：35
                    // 配送费：3
                    //
                    // 商家活动：-1（满20-1）
                    // 代理活动（松江）：-4（满30-4）
                    // 平台活动：-2（满30-2）
                    //
                    // 用户实际支付：35+3-1-4-2 = 31
                    //
                    // 商家收入：        (35  )* 80% -1  = 27
                    // 平台收入：	    (35+3)* 4% -2  = -0.48
                    // 代理收入（安图）： (35+3)* 3% = 1.14           (此代理属于平台)
                    // 代理收入（松江）： 31-27-(-0.48)-1.14 = 3.34   (此代理属于安图)

//                    Double totalMoney = orderInfo.getTotalMoney();//订单总金额
//                    Double platformShareMoney = JYMathDoubleUtils.sub(totalMoney, packetPrice);// 平台及代理分成的的金额是所有金额减掉打包费后的金额
                    Double shareMoney = orderInfo.getTotalMoney();
                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                        Double plateSharePrice = JYMathDoubleUtils.sub(packetPrice, shopSharePacketPrice);
//                        if (plateSharePrice < 0.0d) {
//                            plateSharePrice = 0.0d;
//                        }
                        shareMoney = JYMathDoubleUtils.sub(shareMoney, shopSharePacketPrice);// 店铺分完打包费后 剩余的部分分 由平台以及相关的在此分成
                    } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                    }

                    // 1. 平台分成金额
                    // 平台抽取比例
//                    platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);


                    // 2. 中间代理分成金额
//                    Double cityAdminRate1 = 0d;// 平台下的第一级代理（省代）
//                    Double cityAdminMoney1 = 0d;

//                    Double cityAdminRate2 = 0d;// (市代)
//                    Double cityAdminMoney2 = 0d;

//                    Double cityAdminRate3 = 0d;// (区代)
//                    Double cityAdminMoney3 = 0d;

//                    Double cityAdminRate4 = 0d;// (县代)
//                    Double cityAdminMoney4 = 0d;


                    // 3. 直属代理分成金额
//                    Double cityAdminRate = 0d;//无用了
//                    Double adminShareMoney = 0d;


                    // 城代信息
                    SysCityAdmin cityAdmin = null;
                    if (ValidateUtil.isID(sellerId)) {
                        if (SYS_SETTINGS.SHOP_BELONG_MULTIPLE_AGENT == 1) {
                            // cswm 一个商户属于多个代理计算分成问题

//                            if (ValidateUtil.isAbsList(agentIdList)) {
                            // 获取商圈的直属代理信息
                            cityAdmin = getShopCityAdminByShopId(sellerId);
//                                List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
//                                if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {
//                                    List<SysCityAdmin> adminList = SysCityAdminMemory.getInstance().listOfUsername(adminUsernameList.get(0));
//                                    if (ValidateUtil.isAbsList(adminList) && null != adminList.get(0)) {
//                                        cityAdmin = adminList.get(0);
//                                    }
//                                }
//                            }
                        } else {
                            // 获取店铺所属商圈
                            Long agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(sellerId);
                            if (ValidateUtil.isID(agentId)) {
                                // 获取商圈的直属代理信息
                                List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                                if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {

                                }
                            }
                        }
                    }
                    if (null != cityAdmin) {
                        // 平台分成比例
                        platformRate = cityAdmin.getPlateShareRate();
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                        platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);// 四舍五入

                        // 店铺直属代理是什么级别
                        if (null != cityAdmin.getLevel()) {

                            // 直属代理分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(payMoney, shopMoney);// 支付金额 - 商家分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, postmanMoney);// 支付金额 - 配送员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, businessMoney);// 支付金额 - 业务员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, platformMoney);// 支付金额 - 平台抽成
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);// 支付金额 - 区域主管的抽成

                            //中间代理分成金额
                            switch (cityAdmin.getLevel()) {
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_FORE: {// 目前第四级为顶级代理（即平台下的第一个代理，省代）
                                    // 只有省代参与分成，其余下级代理不分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    // 减掉平台的都是此城代的

                                    // 直接城代数据（不管城代是几级都有数据）
//                                    cityAdminUsername = cityAdmin.getShareUsername1();
//                                    cityAdminMoney = cityAdminMoney;

                                    cityAdminMoney1 = cityAdminMoney;
                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_THREE: {// 当前代理为市级代理
                                    // 省代和市代有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();

                                    cityAdminRate1 = cityAdmin.getShareRate1();

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername2();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成

                                    cityAdminMoney2 = cityAdminMoney;
                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_TWO: {// 当前代理为区级代理
                                    // 省市区有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);

                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername3();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成


                                    cityAdminMoney3 = cityAdminMoney;

                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_ONE: {// 店铺的直属代理 当前代理为乡镇代理
                                    // 省市区有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    cityAdminUsername4 = cityAdmin.getShareUsername4();

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    cityAdminRate3 = cityAdmin.getShareRate3();

                                    // rate4是目前的直属代理

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate3);

                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                                    cityAdminMoney3 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney3);// 四舍五入

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername4();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成

                                    cityAdminMoney4 = cityAdminMoney;
                                }
                                break;
                            }
                        } else {
                            // 无级别，所有金额属于平台
                            platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                        }
                    } else {
                        // 没有直属代理  那么收入全部归于平台
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        // 若没有代理的话  只有店铺和平台的话  这杨计算比较合适
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员
                        platformMoney = JYMathDoubleUtils.sub(platformMoney, agentManagerMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员 - 区域代理的
                        // 下面这样计算  平台得到的钱应该是0 具体算法目的不详
//                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                    }


                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);
                    // 直接城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                    // 相对平台的第一级，即省级代理分成
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);

                    // 相对平台的第二级，即市级代理分成
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);

                    // 相对平台的第三级，即区级代理分成
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);

                    // 相对平台的第四级，即乡镇级代理分成
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN_MANAGE_DELIVERY: { //4除直属代理参与配送费分成，其他代理不参与配送费分成
                    // 例如，商家D，完成了一单
                    // 商品金额：35
                    // 配送费：3
                    //
                    // 商家活动：-1（满20-1）
                    // 代理活动（松江）：-4（满30-4）
                    // 平台活动：-2（满30-2）
                    //
                    // 用户实际支付：35+3-1-4-2 = 31
                    //
                    // 商家收入：        (35  )* 80% -1  = 27
                    // 平台收入：	    (35)* 4% -2  = -0.6
                    // 代理收入（安图）： (35)* 3% = 1.05           (此代理属于平台)
                    // 代理收入（松江）： 31-27-(-0.6)-1.05 = 3.55   (此代理属于安图)


                    platformAwardMoney = 0d;// 平台优惠金额
                    // 平台的折扣金额
                    if (ValidateUtil.isAbsDouble(orderInfo.getDiscountMoney())) {
                        platformAwardMoney = orderInfo.getDiscountMoney();
                    }
                    Double adminAwardMoney = 0d;// 直属代理优惠金额
                    Double adminAwardMoney1 = 0d;// 相对平台第一级
                    Double adminAwardMoney2 = 0d;// 相对平台第二级
                    Double adminAwardMoney3 = 0d;// 相对平台第三级
                    Double adminAwardMoney4 = 0d;// 相对平台第四级
                    List<OrderGameRules> orderGameList = OrderGameMemory.getInstance().listOfOrderNo(orderNo);// 活动信息
                    // 代理承担的活动费用<代理账号，活动费用>
                    HashMap<String, Double> adminAwardMap = new HashMap<>();
                    // 活动信息
                    if (ValidateUtil.isAbsList(orderGameList)) {
                        for (int i = 0; i < orderGameList.size(); i++) {
                            OrderGameRules game = orderGameList.get(i);
                            if (null == game) {
                                continue;
                            }
                            if (null == game.getBossType()) {
                                continue;
                            }
                            switch (game.getBossType()) {
                                case BOOS_TYPE.PLATFORM: {// 平台活动（所有费用平台承担）
                                    // 平台的红包 设置代理承担金额
                                    if (game.getGameType() != null
                                            && (game.getGameType() - GAME_TYPE.RED_PACKET_MONEY == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_GOODS == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_FIRST_MONEY == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_FIRST_GOODS == 0)) {
                                        if (game.getPlatformAward() != null && game.getPlatformAward() >= 0 && game.getCityAdminAward() != null && game.getCityAdminAward() >= 0) {
                                            platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getPlatformAward());
                                            adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getCityAdminAward());
                                        } else {
                                            Double tempAward = game.getAward();
                                            if (ValidateUtil.isNotAbsDouble(tempAward)) {
                                                tempAward = 0.0d;
                                            }
                                            if (ValidateUtil.isAbsDouble(game.getShopAward()) && tempAward > game.getShopAward()) {
                                                tempAward = JYMathDoubleUtils.sub(tempAward, game.getShopAward());
                                            }
                                            if (ValidateUtil.isAbsDouble(game.getCityAdminAward()) && tempAward > game.getCityAdminAward()) {
                                                tempAward = JYMathDoubleUtils.sub(tempAward, game.getCityAdminAward());
                                                adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getCityAdminAward());
                                            }
                                            if (ValidateUtil.isAbsDouble(tempAward)) {
                                                platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, tempAward);
                                            }
                                        }
                                    } else {
                                        if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                            platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getPlatformAward());
                                        } else {
                                            platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getAward());
                                        }
                                    }
                                }
                                break;
                                case BOOS_TYPE.SHOP: {// 商家活动（设置的平台金额，由直属代理承担）
                                    if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                        adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getPlatformAward());
                                    }
                                }
                                break;
                                case BOOS_TYPE.CITY_ADMIN: { // 城代活动（根据账号，谁的就给谁，没有的话就给直属代理）
                                    String adminUsername = game.getAdminUsername();
                                    if (ValidateUtil.isNotNull(adminUsername) && SYS_SETTINGS.CITY_ADMIN_CAN_MANAGE_PACKET == 1
                                            && game.getGameType() != null
                                            && (game.getGameType() - GAME_TYPE.RED_PACKET_MONEY == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_GOODS == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_FIRST_MONEY == 0
                                            || game.getGameType() - GAME_TYPE.RED_PACKET_FIRST_GOODS == 0
                                    )) {
                                        Double adminA = 0d;
                                        if (adminAwardMap.containsKey(adminUsername)) {
                                            adminA = adminAwardMap.get(adminUsername);
                                        }
                                        if (ValidateUtil.isAbsDouble(game.getCityAdminAward())) {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getCityAdminAward());
                                            // 由于上面将平台优惠置位空了 所以在此进行计算
                                            if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                                platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getPlatformAward());
                                            }
                                        } else {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getAward());
                                        }
                                        adminAwardMap.put(adminUsername, adminA);
                                    } else if (ValidateUtil.isNull(adminUsername)) {
                                        if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                            adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getPlatformAward());
                                        } else {
                                            adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getAward());
                                        }
                                    } else {
                                        Double adminA = 0d;
                                        if (adminAwardMap.containsKey(adminUsername)) {
                                            adminA = adminAwardMap.get(adminUsername);
                                        }
                                        if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getPlatformAward());
                                        } else {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getAward());
                                        }
                                        adminAwardMap.put(adminUsername, adminA);
                                    }
                                }
                                break;
                            }
                        }
                        platformAwardMoney = JYMathDoubleUtils.scaleHalfUp(platformAwardMoney);// 四舍五入
                        adminAwardMoney = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney);// 四舍五入
                    }


//                    Double shareMoney = orderInfo.getTotalGoodsMoney();
//
//                    // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
//                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                        Double plateSharePrice = JYMathDoubleUtils.sub(packetPrice, shopSharePacketPrice);// 所有打包费-店铺分去的
//                        if (plateSharePrice < 0.0d) {
//                            plateSharePrice = 0.0d;
//                        }
//                        shareMoney = JYMathDoubleUtils.add(shareMoney, plateSharePrice);// 店铺分完打包费后 剩余的部分分 由平台以及相关的在此分成
//                    } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
//                    }
//
//                    Double noDeliveryShareMoney = shareMoney;


                    Double shareMoney = orderInfo.getTotalMoney();
                    Double noDeliveryShareMoney = JYMathDoubleUtils.sub(shareMoney, deliveryPrice);
//                    Double shareMoney = orderInfo.getTotalGoodsMoney();

//                    // 打包费是否参与分成   0不参与(都归店铺所有)   1参与(同店铺分成比例) 2 按城市统一设置打包费 3 按照对店铺的设置进行分成打打包费
//                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 3 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                        Double plateSharePrice = JYMathDoubleUtils.sub(packetPrice, shopSharePacketPrice);// 所有打包费-店铺分去的
//                        if (plateSharePrice < 0.0d) {
//                            plateSharePrice = 0.0d;
//                        }
//                        shareMoney = JYMathDoubleUtils.add(shareMoney, plateSharePrice);// 店铺分完打包费后 剩余的部分分 由平台以及相关的在此分成
//                    } else if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
//                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
//                    }

//                    Double noDeliveryShareMoney = shareMoney;
//                    Double noDeliveryShareMoney = JYMathDoubleUtils.sub(shareMoney, deliveryPrice);


                    // 城代信息
                    SysCityAdmin cityAdmin = null;
                    if (ValidateUtil.isID(sellerId)) {
                        // 获取店铺所属商圈
                        Long agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(sellerId);
                        if (ValidateUtil.isID(agentId)) {
                            // 获取商圈的直属代理信息
                            List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                            if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {
                            }
                        }
                    }
                    if (null != cityAdmin) {
                        // 平台分成比例
                        platformRate = cityAdmin.getPlateShareRate();
                        if (null == platformRate) {
                            platformRate = 0d;
                        }
                        platformRate = JYMathDoubleUtils.div(platformRate, 100);
                        // 平台收入：	    (商品金额35)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(noDeliveryShareMoney, platformRate), platformAwardMoney);
                        platformMoney = JYMathDoubleUtils.scaleHalfUp(platformMoney);// 四舍五入
                        // 获取此代理所属级别
                        if (null != cityAdmin.getCurrLevel()) {
                            // 直属代理分成金额
                            cityAdminUsername = cityAdmin.getUsername();// 直属代理账号
                            cityAdminMoney = JYMathDoubleUtils.sub(payMoney, shopMoney);// 支付金额 - 商家分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, postmanMoney);// 支付金额 - 配送员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, businessMoney);// 支付金额 - 业务员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, platformMoney);// 支付金额 - 平台抽成
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, agentManagerMoney);// 支付金额 - 区域主管抽成
                            // 代理的钱是按照实际支付金额计算的  所以不用将优惠金额在减去 或者说 支付金额已经取出优惠金额了
//                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, adminAwardMoney);// 支付金额 - 代理优惠金额
                            Double tempAdminAwardMoney = adminAwardMap.get(cityAdminUsername);
                            if (tempAdminAwardMoney != null) {
                                adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, tempAdminAwardMoney);
                                // 计算完直接移除
                                adminAwardMap.remove(cityAdminUsername);
                            }

                            switch (cityAdmin.getCurrLevel()) {
                                case 1: {// 相对于平台的第一级是直属代理
                                    // 平台抽佣金
                                    // 无级别，所有金额属于平台
                                }
                                break;
                                case 2: { // 相对于平台的第2级
                                    // 平台抽佣金、一级代理抽 佣金
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    adminAwardMoney1 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney1);// 四舍五入


                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    // yantk 2019年6月5日13:26:28
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1); // 减去优惠的
                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    // end
                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminRate = cityAdminRate1;
                                }
                                break;
                                case 3: {// 相对于平台的第3级  受益者：平台、第一级、第二级，第三级未直接代理
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }
                                    adminAwardMoney1 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney1);// 四舍五入
                                    adminAwardMoney2 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney2);// 四舍五入

                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);

                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);


                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);

                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                                    // 直接城代数据
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 一级代理抽成
                                    cityAdminRate = cityAdminRate2;
                                }
                                break;
                                case 4: {// 相对于平台的第4级
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    adminAwardMoney3 = adminAwardMap.get(cityAdminUsername3);
                                    if (null == adminAwardMoney3) {
                                        adminAwardMoney3 = 0d;
                                    }
                                    adminAwardMoney1 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney1);// 四舍五入
                                    adminAwardMoney2 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney2);// 四舍五入
                                    adminAwardMoney3 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney3);// 四舍五入

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);
                                    cityAdminRate3 = cityAdmin.getShareRate3();
                                    if (null == cityAdminRate3) {
                                        cityAdminRate3 = 0d;
                                    }
                                    cityAdminRate3 = JYMathDoubleUtils.div(cityAdminRate3, 100);

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate3);
                                    cityAdminMoney3 = JYMathDoubleUtils.sub(cityAdminMoney3, adminAwardMoney3);


                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                                    cityAdminMoney3 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney3);// 四舍五入
                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成

                                    cityAdminRate = cityAdminRate3;
                                }
                                break;
                                case 5: {// 相对于平台的第5级
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    adminAwardMoney3 = adminAwardMap.get(cityAdminUsername3);
                                    if (null == adminAwardMoney3) {
                                        adminAwardMoney3 = 0d;
                                    }
                                    cityAdminUsername4 = cityAdmin.getShareUsername4();
                                    adminAwardMoney4 = adminAwardMap.get(cityAdminUsername4);
                                    if (null == adminAwardMoney4) {
                                        adminAwardMoney4 = 0d;
                                    }
                                    adminAwardMoney1 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney1);// 四舍五入
                                    adminAwardMoney2 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney2);// 四舍五入
                                    adminAwardMoney3 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney3);// 四舍五入
                                    adminAwardMoney4 = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney4);// 四舍五入

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);
                                    cityAdminRate3 = cityAdmin.getShareRate3();
                                    if (null == cityAdminRate3) {
                                        cityAdminRate3 = 0d;
                                    }
                                    cityAdminRate3 = JYMathDoubleUtils.div(cityAdminRate3, 100);
                                    cityAdminRate4 = cityAdmin.getShareRate4();
                                    if (null == cityAdminRate4) {
                                        cityAdminRate4 = 0d;
                                    }
                                    cityAdminRate4 = JYMathDoubleUtils.div(cityAdminRate4, 100);

                                    // rate4是目前的直属代理

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate3);
                                    cityAdminMoney3 = JYMathDoubleUtils.sub(cityAdminMoney3, adminAwardMoney3);
                                    cityAdminMoney4 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate4);
                                    cityAdminMoney4 = JYMathDoubleUtils.sub(cityAdminMoney4, adminAwardMoney4);

                                    cityAdminMoney1 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney1);// 四舍五入
                                    cityAdminMoney2 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney2);// 四舍五入
                                    cityAdminMoney3 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney3);// 四舍五入
                                    cityAdminMoney4 = JYMathDoubleUtils.scaleHalfUp(cityAdminMoney4);// 四舍五入

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney4);// 支付金额 - 四级代理抽成
                                    cityAdminRate = cityAdminRate4;
                                }
                                break;
                            }
                            if (cityAdminUsername1 != null && adminAwardMap.containsKey(cityAdminUsername1)) {
                                adminAwardMap.remove(cityAdminUsername1);
                            }
                            if (cityAdminUsername2 != null && adminAwardMap.containsKey(cityAdminUsername2)) {
                                adminAwardMap.remove(cityAdminUsername2);
                            }
                            if (cityAdminUsername3 != null && adminAwardMap.containsKey(cityAdminUsername3)) {
                                adminAwardMap.remove(cityAdminUsername3);
                            }
                            if (cityAdminUsername4 != null && adminAwardMap.containsKey(cityAdminUsername4)) {
                                adminAwardMap.remove(cityAdminUsername4);
                            }
                            // 将剩余的代理优惠金额 计入到直属代理身上
                            if (!adminAwardMap.isEmpty()) {
                                for (Double temp : adminAwardMap.values()) {
                                    if (ValidateUtil.isAbsDouble(temp)) {
                                        adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, temp);
                                    }
                                }
                                adminAwardMoney = JYMathDoubleUtils.scaleHalfUp(adminAwardMoney);
                            }
                        }
                    } else {
                        // 没有直属代理  那么收入全部归于平台
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        // 若没有代理的话  只有店铺和平台的话  这杨计算比较合适
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员
                        // 区域主管
                        platformMoney = JYMathDoubleUtils.sub(platformMoney, agentManagerMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员 - 减去 区域代理的钱

                        // 由于没有代理参与分成,故将代理的优惠增加平台的优惠金额
                        // 1. 直属代理的金额增加进去计算
                        if (ValidateUtil.isAbsDouble(adminAwardMoney)) {
                            platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, adminAwardMoney);
                        }
                        // 2. 其他代理的金额也增加计算
                        if (!adminAwardMap.isEmpty()) {
                            for (Double temp : adminAwardMap.values()) {
                                if (ValidateUtil.isAbsDouble(temp)) {
                                    platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, temp);
                                }
                            }
                            platformAwardMoney = JYMathDoubleUtils.scaleHalfUp(platformAwardMoney);
                        }
                        // 下面这样计算  平台得到的钱应该是0 具体算法目的不详
//                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                    }


                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformAwardMoney(platformAwardMoney);
                    orderPayInfo.setPlatformRate(platformRate);
                    // 直接城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(cityAdminRate);
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);
                    orderPayInfo.setCityAdminAward(adminAwardMoney);

                    // 相对平台的第一级，即省级代理分成
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);
                    orderPayInfo.setCityAdminAward1(adminAwardMoney1);

                    // 相对平台的第二级，即市级代理分成
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);
                    orderPayInfo.setCityAdminAward2(adminAwardMoney2);

                    // 相对平台的第三级，即区级代理分成
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);
                    orderPayInfo.setCityAdminAward3(adminAwardMoney3);

                    // 相对平台的第四级，即乡镇级代理分成
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                    orderPayInfo.setCityAdminAward4(adminAwardMoney4);
                }
                break;
            }
        }


        //外币记录
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) {//是否使用主币支付 1使用主币支付 0使用外币支付
            //外币支付的
            if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                //外币汇率
                orderPayInfo.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
            }
            // 使用外币支付
            orderPayInfo.setUseMainCurrencyPay(0);
        } else {
            orderPayInfo.setUseMainCurrencyPay(1);
        }
        // 总的抽成的钱
        orderPayInfo.setPlateDrawGoodMoney(plateGoodDrawMoney).setTotalDrawGoodsMoney(totalGoodsDrawMoney);
        if (SYS_SETTINGS.IS_OPEN_SHOP_SETTLEMENT == 1
                && orderInfo.getOrderType() != null && (orderInfo.getOrderType() - ORDER_TYPE.SHOP_GOODS == 0 || orderInfo.getOrderType() - ORDER_TYPE.GROUP_ORDER == 0)) {
            // 将入账统计,入账状态设置为0 表示未统计未入账
            orderPayInfo.setStatisticState(0).setSettlementState(0);
        }
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("创建流水失败");
        }
        //缓存流水
        OrderPayMemory.getInstance().add(orderNo);// 分成
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.ORDER_PAY_ADD).setStr01(orderNo);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        return new ResultInfo();
    }

    // 累计抽成多少钱
    public static double calculateTotalGoodsDrawMoney(String orderNo, Double minPrice) {
        if (StringUtils.isEmpty(orderNo) || minPrice == null) { // 设置了多少钱就多少钱  至于加钱还是减去钱 和算法没有关系 （售后要求修改）
//        if (StringUtils.isEmpty(orderNo) || minPrice == null || minPrice <= 0.0d) {
            return 0.0d;
        }
        double drawGoodsMoney = 0.0d;
        List<OrderGoods> orderGoodsList = OrderGoodsMemory.getInstance().listOfOrderNo(orderNo);
        if (ValidateUtil.isAbsList(orderGoodsList)) {
            for (OrderGoods orderGoods : orderGoodsList) {
                if (orderGoods == null) {
                    continue;
                }
                if (orderGoods.getGoodsPrice() > minPrice && orderGoods.getTotalCount() > 0) {
                    drawGoodsMoney = JYMathDoubleUtils.add(JYMathDoubleUtils.mul(orderGoods.getTotalCount(), minPrice), drawGoodsMoney);
                }
            }
        }
        return drawGoodsMoney;
    }

    // 获取商家所属代理信息
    public static SysCityAdmin getShopCityAdminByShopId(Long sellerId) {
        SysCityAdmin sysCityAdminResult = null;
        if (SYS_SETTINGS.SHOP_BELONG_MULTIPLE_AGENT == 1) {
            // 一个商家属于多个商圈模式  可能会出现多个代理  如果商家出现所属代理则匹配 若没有 直接但会第一条
            ShopInfo shopInfo = ShopMemory.getInstance().getShopById(sellerId);
            List<Long> agentIdList = ShopAgentRelationMemory.getInstance().agentListOfShopId(sellerId);
            if (agentIdList == null || agentIdList.isEmpty()) {
                return null;
            }
            List<String> cityAdminList = new ArrayList<>();
            for (Long agentId : agentIdList) {
                List<String> tempList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                if (tempList != null && !tempList.isEmpty()) {
                    cityAdminList.addAll(tempList);
                }
            }
            Set<String> cityAdminSet = cityAdminList.stream().collect(Collectors.toSet());
            if (cityAdminSet != null && !cityAdminSet.isEmpty()) {
                List<SysCityAdmin> sysCityAdminS = cityAdminSet.stream().map(a -> getByCityAdminUsername(a)).filter(vo -> vo != null).collect(Collectors.toList());
                if (sysCityAdminS != null && sysCityAdminS.size() > 0) {
                    if (Optional.ofNullable(shopInfo).map(ShopInfo::getCityAdmin).isPresent()) {
                        for (SysCityAdmin sysCityAdmin : sysCityAdminS) {
                            if (sysCityAdmin != null && shopInfo.getCityAdmin().equals(sysCityAdmin.getUsername())) {
                                sysCityAdminResult = sysCityAdmin;
                                break;
                            }
                        }
                        // 都没有匹配上  则返回第一个
                        if (sysCityAdminResult == null) {
                            sysCityAdminResult = sysCityAdminS.get(0);
                        }
                    } else {
                        sysCityAdminResult = sysCityAdminS.get(0);
                    }
                }
            }
        } else {
            Long agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(sellerId);
            if (ValidateUtil.isID(agentId)) {
                // 获取商圈的直属代理信息
                List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {
                }
            }
        }
        return sysCityAdminResult;
    }

    public static SysCityAdmin getByCityAdminUsername(String username) {
        return null;
    }


    //--------------------集合订单分成
    // 生鲜版，有父子订单分成
    public static ResultInfo addOrderPayMulti(OrderInfo orderInfo, ShopInfo shopInfo, String payerAppId, String payerUserAccount) {

        if (null == orderInfo) {
            System.out.println("分成订单不存在");
            return new ResultInfo("分成订单不存在");
        }
        String orderNo = orderInfo.getOrderNo();
        String username = orderInfo.getUsername();


        if (OrderMan.isMultipleParentOrder(orderNo)) {
            // 只有平台活动，按子订单分成
            HashSet<String> set = OrderInfoMemory.getInstance().listOfParentOrderNo(orderNo);
            if (null != set && set.size() > 0) {
                // 店铺分成
                for (String childOrderNo : set) {
                    if (ValidateUtil.isNull(childOrderNo)) {
                        continue;
                    }
                    OrderInfo childOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(childOrderNo);
                    if (null == childOrderInfo) {
                        continue;
                    }
                    Long shopId = childOrderInfo.getShopId();
                    if (ValidateUtil.isNotID(shopId)) {
                        continue;
                    }
                    ShopInfo childShopInfo = ShopMemory.getInstance().getShopById(shopId);
                    if (null == childShopInfo) {
                        continue;
                    }
                    // 分成金额
                    ResultInfo resultInfo = addNormalOrderPayV2(childOrderNo, orderInfo.getPayType(), payerAppId, payerUserAccount, username, childOrderInfo, childShopInfo);
                    if (null == resultInfo) {
                        return new ResultInfo("增加流水失败，请稍后再试。");
                    } else {
                        if (!resultInfo.isSuccess()) {
                            System.out.println("订单支付成功后增加流水失败");
                            return resultInfo;
                        }
                    }
                }

                // 代理、平台分成、配送
                ResultInfo resultInfoPlat = addMultiPlatOrderPay(orderInfo, set, orderInfo.getPayType(), payerAppId, payerUserAccount, orderInfo.getUsername());
                if (null == resultInfoPlat) {
                    return new ResultInfo("增加流水失败，请稍后再试。 plat");
                } else {
                    if (!resultInfoPlat.isSuccess()) {
                        System.out.println("订单支付成功后增加流水失败 plat:" + orderNo);
                        return resultInfoPlat;
                    }
                }
            } else {
                System.out.println("子订单不存在，故未分成：" + orderNo);
            }


        } else {
            // 分成金额
            ResultInfo resultInfo = OrderPay.addOrderPay(orderNo, orderInfo.getPayType(), payerAppId, payerUserAccount, username, orderInfo, shopInfo);// 订单回调业务处理
            if (null == resultInfo) {
                return new ResultInfo("增加流水失败，请稍后再试。");
            } else {
                if (!resultInfo.isSuccess()) {
                    System.out.println("订单支付成功后增加流水失败");
                    return resultInfo;
                }
            }
        }


        return new ResultInfo();
    }

    // 生鲜版，平台和代理的收入（店铺的收入已单独计算完成）
    public static ResultInfo addMultiPlatOrderPay(OrderInfo orderInfo, HashSet<String> childSet, String payType, String payerAppId, String payerUserAccount, String username) {
        if (null == orderInfo) {
            return new ResultInfo("分成出错，订单信息为空·");
        }
        String orderNo = orderInfo.getOrderNo();
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单编号不能为空：[addMultiPlatOrderPay]");
        }

        //add by zhuwx 2018-9-25
        OrderPayInfo orderPayInfoR = OrderPayMemory.getInstance().get(orderNo);
        if (null != orderPayInfoR) {
            return new ResultInfo("已经分成过了");
        }

        //商品总金额
        Double totalGoodsMoney = orderInfo.getTotalGoodsMoney();//商品的金额
        //支付金额
        Double payMoney = orderInfo.getTotalPrice();//用户支付的金额
        //配送费
        Double deliveryPrice = orderInfo.getDeliveryPrice();//配送费
        Double packetPrice = orderInfo.getPacketPrice();//打包费

        // 税费
        Double taxMoney = orderInfo.getGoodsTaxMoney();

        if (null == taxMoney) {
            taxMoney = 0d;
        }
        if (null == payMoney) {
            payMoney = 0d;
        }
        if (null == deliveryPrice) {
            deliveryPrice = 0d;
        }
        if (null == packetPrice) {
            packetPrice = 0d;
        }
        if (ValidateUtil.isNotAbsDouble(totalGoodsMoney)) {
            if (null == deliveryPrice) {
                orderInfo.setDeliveryPrice(0d);
            }
            if (null == packetPrice) {
                orderInfo.setPacketPrice(0d);
            }
            if (ValidateUtil.isNotAbsDouble(orderInfo.getTotalMoney())) {
                if (ValidateUtil.isAbsDouble(payMoney)) {
                    orderInfo.setTotalMoney(JYMathDoubleUtils.add(JYMathDoubleUtils.add(payMoney, deliveryPrice), packetPrice));
                }
            }
            totalGoodsMoney = JYMathDoubleUtils.sub(orderInfo.getTotalMoney(), deliveryPrice);
        }

        Double shopRate = 0d;
        Double shopTotalMoney = totalGoodsMoney;//店铺实际订单金额 未分成前（商品总金额 加上应该加的配送费 减掉店铺减的金额）
        Double shopMoney = 0d;//店铺实际收入 分成后
        Double postmanRate = 0d;
        Double postmanMoney = 0d;

        //活动金额处理
        // 店铺活动金额
        Double shopAwardMoney = orderInfo.getShopAwardMoney();//活动满减金额-店铺减金额
        // 平台活动金额
        Double platformAwardMoney = orderInfo.getPlatformAwardMoney();//活动满减金额-平台减金额


        Double businessMoney = 0d;//业务员分成金额

        // -----------1. 店铺分成
        // -----------2. 骑手分成
        // -----------3. 业务员分成
        // 获取该订单所有店铺、骑手已分成金额
        if (null != childSet && childSet.size() > 0) {
            // 店铺分成
            for (String childOrderNo : childSet) {
                if (ValidateUtil.isNull(childOrderNo)) {
                    continue;
                }

                OrderPayInfo childPay = OrderPayMemory.getInstance().get(childOrderNo);
                if (null == childPay) {
                    continue;
                }

                if (ValidateUtil.isAbsDouble(childPay.getShopMoney())) {
                    shopMoney = JYMathDoubleUtils.add(shopMoney, childPay.getShopMoney());
                }
//                if (ValidateUtil.isAbsDouble(childPay.getPostmanMoney())) {
//                    postmanMoney = JYMathDoubleUtils.add(postmanMoney, childPay.getPostmanMoney());
//                }
                if (ValidateUtil.isAbsDouble(childPay.getBusinessMoney())) {
                    businessMoney = JYMathDoubleUtils.add(businessMoney, childPay.getBusinessMoney());
                }
            }
        }

        if (0 == orderInfo.getIsZiQu() && OrderMan.isMultipleParentOrder(orderNo)) { //配送的订单才进来
            if (null == SYS_SETTINGS.POSTMAN_SHARE_MONEY) {
                System.out.println("未设置配送员是否参与分成");
                return new ResultInfo("操作失败，请稍后再试。4");
            }


            Integer postManShare = SYS_SETTINGS.POSTMAN_SHARE_MONEY; //配送员分成类型（0不参与，1总金额，2参与配送费部分）
            switch (postManShare) {
                case SysSettingValueKey.POSTMAN_SHARE_NONE: {//不参与分成
                    postmanRate = 0d;
                }
                break;
                case SysSettingValueKey.POSTMAN_SHARE_ALL: {//参与订单总金额分成
                    Double totalMoney = orderInfo.getTotalMoney();//订单总金额
                    //配送员参与分成比例
                    if (null == SYS_SETTINGS.POSTMAN_RATE) {
                        System.out.println("未设置配送员分成比例");
                        return new ResultInfo("操作失败，请稍后再试。5");
                    }
                    postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                    if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
                        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
                        if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
                            postmanRate = cityPostman.getMoneyRate();
                        }
                    }

                    if (ValidateUtil.isAbsDouble(postmanRate)) {
                        postmanMoney = JYMathDoubleUtils.mul(totalMoney, JYMathDoubleUtils.div(postmanRate, 100));
                    }
                }
                break;
                case SysSettingValueKey.POSTMAN_SHARE_DELIVERY: { //参与配送费部分
                    if (null == SYS_SETTINGS.POSTMAN_RATE) {
                        System.out.println("未设置配送员分成比例");
                        return new ResultInfo("操作失败，请稍后再试。5");
                    }
                    postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
                    if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
                        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
                        if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
                            postmanRate = cityPostman.getMoneyRate();
                        }
                    }

                    if (ValidateUtil.isAbsDouble(postmanRate)) {
                        postmanMoney = JYMathDoubleUtils.mul(deliveryPrice, JYMathDoubleUtils.div(postmanRate, 100));
                    }
                }
                break;
                case SysSettingValueKey.POSTMAN_SHARE_BY_AGENT: {// 3按商圈设置的配送员工资结算公式计算
                    //获取订单所在商圈
                    if (ValidateUtil.isID(orderInfo.getAgentId())) {
                        CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
                        //获取商圈配送员费用结算公式
                        if (null == cityAgent) {
                            System.out.println("订单商圈不存在。无法完成配送员分成：" + orderNo);
                            return new ResultInfo("[分成 3]订单经纬度为空，无法计算距离。");
                        }

                        if (ValidateUtil.isNotLatLng(orderInfo.getLat(), orderInfo.getLng())) {
                            System.out.println("订单经纬度为空，无法计算距离：" + orderNo);
                            return new ResultInfo("[分成 5]订单经纬度为空，无法计算距离。");
                        }
                        if (ValidateUtil.isNotLatLng(orderInfo.getShopLat(), orderInfo.getShopLng())) {
                            System.out.println("订单商家经纬度为空，无法计算距离：" + orderNo);
                            return new ResultInfo("[分成 6]操作失败，请稍后再试。");
                        }

                        //配送距离
                        Double distance = Geohash.distance(orderInfo.getLat(), orderInfo.getLng(), orderInfo.getShopLat(), orderInfo.getShopLng());//根据两点的经纬度计算距离
                        Double km = JYMathDoubleUtils.div(distance, 1000);//距离
                        //withinDistance 范围
                        Double psWithinDistance = cityAgent.getPsWithinDistance();
                        if (null == psWithinDistance) {
                            psWithinDistance = 0d;
                        }
                        //fixedCost 范围内价格
                        Double psFixedCost = cityAgent.getPsFixedCost();
                        if (null == psFixedCost) {
                            psFixedCost = 0d;
                        }
                        //oneKmCost 超出后每公里价格
                        Double psOneKmCost = cityAgent.getPsOneKmCost();
                        if (null == psOneKmCost) {
                            psOneKmCost = 0d;
                        }
                        //骑手工资-最大金额数（-1为不限）
                        Double psMaxMoney = cityAgent.getPsMaxMoney();
                        if (null == psMaxMoney) {
                            psMaxMoney = -1d;
                        }
                        if (JYMathDoubleUtils.sub(km, psWithinDistance) <= 0) {
                            postmanMoney = psWithinDistance;
                        } else {
                            Double after = JYMathDoubleUtils.sub(km, psWithinDistance);
                            Double round = Math.ceil(after);
                            postmanMoney = JYMathDoubleUtils.add(psFixedCost, JYMathDoubleUtils.mul(round, psOneKmCost));

                            if (psMaxMoney >= 0 && JYMathDoubleUtils.sub(postmanMoney, psMaxMoney) > 0) {
                                postmanMoney = psMaxMoney;
                            }
                        }
                    } else {
                        //订单商圈不存在，无法完成配送员分成
                        System.out.println("订单商圈不存在，无法完成配送员分成：" + orderNo);
                        return new ResultInfo("订单商圈不存在，无法完成配送员分成。" + orderNo);
                    }
                }
                break;
            }

        }


        Double platformRate = 0d;//平台分成比例
        Double platformMoney = 0d;//平台分成金额
        OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getUsername(), null, null, payType, 1, username, 0, DateUtils.getCurrTime(),
                JYMathDoubleUtils.div(shopRate, 100), shopMoney, JYMathDoubleUtils.div(postmanRate, 100), postmanMoney, JYMathDoubleUtils.div(platformRate, 100), platformMoney,
                shopAwardMoney, platformAwardMoney);
        orderPayInfo.setBuyerAppId(payerAppId);
        orderPayInfo.setBuyerLogonId(payerUserAccount);

        //4. 城代、平台分成
        String cityAdminUsername = null; //城代账号
        Double cityAdminRate = 0d; //城代分成比例
        Double cityAdminMoney = 0d;//城代分成金额

        String cityAdminUsername1 = null; //城代账号
        Double cityAdminRate1 = 0d; // 直系城代分成比例
        Double cityAdminMoney1 = 0d;// 直系城代分成金额
        String cityAdminUsername2 = null; //城代账号
        Double cityAdminRate2 = 0d; // 二级城代分成比例
        Double cityAdminMoney2 = 0d;//二级城代分成金额
        String cityAdminUsername3 = null; //城代账号
        Double cityAdminRate3 = 0d; //城代分成比例
        Double cityAdminMoney3 = 0d;//城代分成金额
        String cityAdminUsername4 = null; //城代账号
        Double cityAdminRate4 = 0d; //城代分成比例
        Double cityAdminMoney4 = 0d;//城代分成金额
        if (null != SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY) {
            //0不抽取，1只有店铺和平台方式，2平台统一设置，最后金额归属平台，3店铺的直属代理是最大受益人
            switch (SYS_SETTINGS.IS_SYS_CITY_ADMIN_SHARE_MONEY) {
                case CITY_ADMIN_SHARE_MONEY_TYPE.NONE: {//0不抽取
                    //平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员
                    platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员-分给业务员

                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN: { //1只有店铺和平台方式
                    ResultInfo resultInfo = getPlateShareCityAdminRate(orderInfo.getAgentId(), orderInfo.getShopId(), orderInfo.getShopCity());//平台抽取代理分成比例
                    if (null != resultInfo && resultInfo.isSuccess()) {
                        platformRate = (Double) resultInfo.getObj();
                        if (ValidateUtil.isNotAbsDouble(platformRate)) {
                            platformRate = 100d;
                        }
                        cityAdminUsername = resultInfo.getMessage();
                    } else {
                        platformRate = 100d;
                    }

                    cityAdminRate = JYMathDoubleUtils.sub(100, platformRate);
                    switch (SYS_SETTINGS.PLAT_SHARE_CITY_ADMIN_MONEY_TYPE) {//平台抽取城代方式
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.NONE: {//不抽取
//                    cityAdminRate = 1d;//平台不抽取的话，所有的盈利都是代理的，这个1也不准确
                            platformRate = 0d;
                            cityAdminRate = 0d;
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformAwardMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.TOTAL_MONEY: {//抽取订单总金额部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getTotalMoney(), JYMathDoubleUtils.div(platformRate, 100));
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.DELIVER_MONEY: {//抽取配送费部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getDeliveryPrice(), JYMathDoubleUtils.div(platformRate, 100));
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                        }
                        break;
                        case PLAT_SHARE_CITY_ADMIN_MONEY_TYPE.PAY_MONEY: {//抽取支付金额部分
                            platformMoney = JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), JYMathDoubleUtils.div(platformRate, 100));
                            // 代理收入 = 支付金额-分成给店铺-分成给配送员-分给业务员-分给平台的-活动减金额
                            cityAdminMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney), platformMoney), platformAwardMoney);
                        }
                        break;
                    }

                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);

                    //城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.PLATFORM_MANAGE: {//2平台统一设置，最后剩余的金额归属平台
                    System.out.println("城市代理参与分成");
                    Long pId = null;
//            sellerId // 店铺id
                    Double shareMoney = totalGoodsMoney;
                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                    }
                    // 减去店铺活动的钱
                    shareMoney = JYMathDoubleUtils.sub(shareMoney, shopAwardMoney);
                    // 由城市获取商圈
                    // 获取店铺属于的商圈
                    Long agentId = orderInfo.getAgentId();
                    List<String> userAdminList = null;
                    if (ValidateUtil.isID(agentId)) {
                        userAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                    }
                    // 由商圈获取所属的 管理者
                    List<SysCityAdmin> sysCityAdminList = null;
                    if (ValidateUtil.isAbsList(userAdminList)) {
                        String useradmin = userAdminList.get(0);
                    }
                    // 所述的
                    if (ValidateUtil.isAbsList(sysCityAdminList)) {
                        pId = sysCityAdminList.get(0).getId();
                    }

//            CityAdminShopRelation byShopId = CityAdminShopRelationMemory.getInstance().getByShopId(sellerId);
                    if (pId != null) {
                        // 一级
                        SysCityAdmin byId = null;
                        if (byId != null) {
                            pId = byId.getPId();
//                    if (ValidateUtil.isAbsDouble(byId.getShareRate1()) && byId.getShareRate1() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                            if (null != byId.getShareRate1() && byId.getShareRate1() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                cityAdminUsername1 = byId.getUsername();
                                cityAdminRate1 = JYMathDoubleUtils.div(byId.getShareRate1(), 100);
                                if (null != cityAdminRate1 && cityAdminRate1 >= 0) {
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                }
                            }
                        } else {
                            pId = null;
                        }
                        // 二级代理的
                        if (ValidateUtil.isID(pId)) {
                            if (byId != null) {
                                pId = byId.getPId();
//                        if (ValidateUtil.isAbsDouble(byId.getShareRate2()) && byId.getShareRate2() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                if (byId.getShareRate2() != null && byId.getShareRate2() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername2 = byId.getUsername();
                                    cityAdminRate2 = JYMathDoubleUtils.div(byId.getShareRate2(), 100);
                                    if (cityAdminRate2 != null && cityAdminRate2 >= 0) {
                                        cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        // 三级代理的
                        if (ValidateUtil.isID(pId)) {
                            if (byId != null) {
                                pId = byId.getPId();
//                        if (ValidateUtil.isAbsDouble(byId.getShareRate3()) && byId.getShareRate3() > 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                if (null != byId.getShareRate3() && byId.getShareRate3() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername3 = byId.getUsername();
                                    cityAdminRate3 = JYMathDoubleUtils.div(byId.getShareRate3(), 100);
//                            if (ValidateUtil.isAbsDouble(cityAdminRate3) && cityAdminRate3 > 0) {
                                    if (null != cityAdminRate3 && cityAdminRate3 >= 0) {
                                        cityAdminMoney3 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate3);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        if (ValidateUtil.isID(pId)) {
                            if (byId != null) {
                                pId = byId.getPId();
                                if (null != byId.getShareRate4() && byId.getShareRate4() >= 0 && ValidateUtil.isNotNull(byId.getUsername())) {
                                    cityAdminUsername4 = byId.getUsername();
                                    cityAdminRate4 = JYMathDoubleUtils.div(byId.getShareRate4(), 100);
//                            if (ValidateUtil.isAbsDouble(cityAdminRate4) && cityAdminRate4 > 0) {
                                    if (null != cityAdminRate4 && cityAdminRate4 >= 0) {
                                        cityAdminMoney4 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate4);
                                    }
                                }
                            } else {
                                pId = null;
                            }
                        }
                        // 先减去 店铺的
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney), businessMoney);
                        // 在减去代理的
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(platformMoney, cityAdminMoney1), cityAdminMoney2), cityAdminMoney3), cityAdminMoney4);
                    }

                    // 平台分成
                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);

                    //城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                    // 直接城市级联分成 （镇代理）
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);

                    // 二级城市代理 （县代理）
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);

                    // 三级城市代理 （市代理）
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);

                    // 四级城市代理 （省代理）
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN_MANAGE: { //3店铺的直属代理是最大受益人
                    // 例如，商家D，完成了一单
                    // 商品金额：35
                    // 配送费：3
                    //
                    // 商家活动：-1（满20-1）
                    // 代理活动（松江）：-4（满30-4）
                    // 平台活动：-2（满30-2）
                    //
                    // 用户实际支付：35+3-1-4-2 = 31
                    //
                    // 商家收入：        (35  )* 80% -1  = 27
                    // 平台收入：	    (35+3)* 4% -2  = -0.48
                    // 代理收入（安图）： (35+3)* 3% = 1.14           (此代理属于平台)
                    // 代理收入（松江）： 31-27-(-0.48)-1.14 = 3.34   (此代理属于安图)

//                    Double totalMoney = orderInfo.getTotalMoney();//订单总金额
//                    Double platformShareMoney = JYMathDoubleUtils.sub(totalMoney, packetPrice);// 平台及代理分成的的金额是所有金额减掉打包费后的金额
                    Double shareMoney = orderInfo.getTotalMoney();
                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                    }

                    // 1. 平台分成金额
                    // 平台抽取比例
//                    platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);


                    // 2. 中间代理分成金额
//                    Double cityAdminRate1 = 0d;// 平台下的第一级代理（省代）
//                    Double cityAdminMoney1 = 0d;

//                    Double cityAdminRate2 = 0d;// (市代)
//                    Double cityAdminMoney2 = 0d;

//                    Double cityAdminRate3 = 0d;// (区代)
//                    Double cityAdminMoney3 = 0d;

//                    Double cityAdminRate4 = 0d;// (县代)
//                    Double cityAdminMoney4 = 0d;


                    // 3. 直属代理分成金额
//                    Double cityAdminRate = 0d;//无用了
//                    Double adminShareMoney = 0d;


                    // 城代信息
                    SysCityAdmin cityAdmin = null;
                    Long agentId = orderInfo.getAgentId();
                    if (ValidateUtil.isID(agentId)) {
                        // 获取商圈的直属代理信息
                        List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                        if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {
                        }
                    }
                    if (null != cityAdmin) {
                        // 平台分成比例
                        platformRate = cityAdmin.getPlateShareRate();
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);

                        // 店铺直属代理是什么级别
                        if (null != cityAdmin.getLevel()) {

                            // 直属代理分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(payMoney, shopMoney);// 支付金额 - 商家分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, postmanMoney);// 支付金额 - 配送员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, businessMoney);// 支付金额 - 业务员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, platformMoney);// 支付金额 - 平台抽成

                            //中间代理分成金额
                            switch (cityAdmin.getLevel()) {
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_FORE: {// 目前第四级为顶级代理（即平台下的第一个代理，省代）
                                    // 只有省代参与分成，其余下级代理不分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    // 减掉平台的都是此城代的

                                    // 直接城代数据（不管城代是几级都有数据）
//                                    cityAdminUsername = cityAdmin.getShareUsername1();
//                                    cityAdminMoney = cityAdminMoney;

                                    cityAdminMoney1 = cityAdminMoney;
                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_THREE: {// 当前代理为市级代理
                                    // 省代和市代有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();

                                    cityAdminRate1 = cityAdmin.getShareRate1();

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername2();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成

                                    cityAdminMoney2 = cityAdminMoney;
                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_TWO: {// 当前代理为区级代理
                                    // 省市区有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername3();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成


                                    cityAdminMoney3 = cityAdminMoney;

                                }
                                break;
                                case CITY_ADMIN_LEVEL_TYPE.CITY_ADMIN_LEVEL_TYPE_ONE: {// 店铺的直属代理 当前代理为乡镇代理
                                    // 省市区有分成
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    cityAdminUsername4 = cityAdmin.getShareUsername4();

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    cityAdminRate3 = cityAdmin.getShareRate3();

                                    // rate4是目前的直属代理

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(shareMoney, cityAdminRate3);


                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminUsername = cityAdmin.getShareUsername4();
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成

                                    cityAdminMoney4 = cityAdminMoney;
                                }
                                break;
                            }
                        } else {
                            // 无级别，所有金额属于平台
                            platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                        }
                    } else {
                        // 没有直属代理  那么收入全部归于平台
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                    }


                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformRate(platformRate);
                    // 直接城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(JYMathDoubleUtils.div(cityAdminRate, 100));
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);

                    // 相对平台的第一级，即省级代理分成
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);

                    // 相对平台的第二级，即市级代理分成
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);

                    // 相对平台的第三级，即区级代理分成
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);

                    // 相对平台的第四级，即乡镇级代理分成
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                }
                break;
                case CITY_ADMIN_SHARE_MONEY_TYPE.CITY_ADMIN_MANAGE_DELIVERY: { //4除直属代理参与配送费分成，其他代理不参与配送费分成
                    // 例如，商家D，完成了一单
                    // 商品金额：35
                    // 配送费：3
                    //
                    // 商家活动：-1（满20-1）
                    // 代理活动（松江）：-4（满30-4）
                    // 平台活动：-2（满30-2）
                    //
                    // 用户实际支付：35+3-1-4-2 = 31
                    //
                    // 商家收入：        (35  )* 80% -1  = 27
                    // 平台收入：	    (35)* 4% -2  = -0.6
                    // 代理收入（安图）： (35)* 3% = 1.05           (此代理属于平台)
                    // 代理收入（松江）： 31-27-(-0.6)-1.05 = 3.55   (此代理属于安图)


                    platformAwardMoney = 0d;// 平台优惠金额
                    Double adminAwardMoney = 0d;// 直属代理优惠金额
                    Double adminAwardMoney1 = 0d;// 相对平台第一级
                    Double adminAwardMoney2 = 0d;// 相对平台第二级
                    Double adminAwardMoney3 = 0d;// 相对平台第三级
                    Double adminAwardMoney4 = 0d;// 相对平台第四级
                    List<OrderGameRules> orderGameList = OrderGameMemory.getInstance().listOfOrderNo(orderNo);// 活动信息
                    // 代理承担的活动费用<代理账号，活动费用>
                    HashMap<String, Double> adminAwardMap = new HashMap<>();
                    // 活动信息
                    if (ValidateUtil.isAbsList(orderGameList)) {
                        for (int i = 0; i < orderGameList.size(); i++) {
                            OrderGameRules game = orderGameList.get(i);
                            if (null == game) {
                                continue;
                            }
                            if (null == game.getBossType()) {
                                continue;
                            }
                            switch (game.getBossType()) {
                                case BOOS_TYPE.PLATFORM: {// 平台活动（所有费用平台承担）
                                    if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                        platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getPlatformAward());
                                    } else {
                                        platformAwardMoney = JYMathDoubleUtils.add(platformAwardMoney, game.getAward());
                                    }
                                }
                                break;
                                case BOOS_TYPE.SHOP: {// 商家活动（设置的平台金额，由直属代理承担）
                                    if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                        adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getPlatformAward());
                                    }
                                }
                                break;
                                case BOOS_TYPE.CITY_ADMIN: { // 城代活动（根据账号，谁的就给谁，没有的话就给直属代理）

                                    String adminUsername = game.getAdminUsername();
                                    if (ValidateUtil.isNull(adminUsername)) {
                                        if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                            adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getPlatformAward());
                                        } else {
                                            adminAwardMoney = JYMathDoubleUtils.add(adminAwardMoney, game.getAward());
                                        }
                                    } else {
                                        Double adminA = 0d;
                                        if (adminAwardMap.containsKey(adminUsername)) {
                                            adminA = adminAwardMap.get(adminUsername);
                                        }
                                        if (ValidateUtil.isAbsDouble(game.getPlatformAward())) {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getPlatformAward());
                                        } else {
                                            adminA = JYMathDoubleUtils.add(adminA, game.getAward());
                                        }
                                        adminAwardMap.put(adminUsername, adminA);
                                    }
                                }
                                break;
                            }
                        }
                    }


//                    Double shareMoney = orderInfo.getTotalMoney();
                    Double shareMoney = orderInfo.getTotalGoodsMoney();
                    if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
                        shareMoney = JYMathDoubleUtils.add(shareMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
                    }

                    Double noDeliveryShareMoney = shareMoney;
//                    Double noDeliveryShareMoney = JYMathDoubleUtils.sub(shareMoney, deliveryPrice);

                    // 城代信息
                    SysCityAdmin cityAdmin = null;
                    // 获取店铺所属商圈
                    Long agentId = orderInfo.getAgentId();
                    if (ValidateUtil.isID(agentId)) {
                        // 获取商圈的直属代理信息
                        List<String> adminUsernameList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                        if (ValidateUtil.isAbsList(adminUsernameList) && null != adminUsernameList.get(0)) {
                        }
                    }
                    if (null != cityAdmin) {
                        // 平台分成比例
                        platformRate = cityAdmin.getPlateShareRate();
                        if (null == platformRate) {
                            platformRate = 0d;
                        }
                        platformRate = JYMathDoubleUtils.div(platformRate, 100);
                        // 平台收入：	    (商品金额35)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(noDeliveryShareMoney, platformRate), platformAwardMoney);

                        // 获取此代理所属级别
                        if (null != cityAdmin.getCurrLevel()) {
                            // 直属代理分成金额
                            cityAdminUsername = cityAdmin.getUsername();// 直属代理账号
                            cityAdminMoney = JYMathDoubleUtils.sub(payMoney, shopMoney);// 支付金额 - 商家分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, postmanMoney);// 支付金额 - 配送员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, businessMoney);// 支付金额 - 业务员分成金额
                            cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, platformMoney);// 支付金额 - 平台抽成

                            switch (cityAdmin.getCurrLevel()) {
                                case 1: {// 相对于平台的第一级是直属代理
                                    // 平台抽佣金
                                    // 无级别，所有金额属于平台
                                }
                                break;
                                case 2: { // 相对于平台的第2级
                                    // 平台抽佣金、一级代理抽 佣金
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }

                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    // yantk 2019年6月5日13:26:28
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1); // 减去优惠的
                                    // end
                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminRate = cityAdminRate1;
                                }
                                break;
                                case 3: {// 相对于平台的第3级  受益者：平台、第一级、第二级，第三级未直接代理
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }

                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);

                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);


                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);

                                    // 直接城代数据
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 一级代理抽成
                                    cityAdminRate = cityAdminRate2;
                                }
                                break;
                                case 4: {// 相对于平台的第4级
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    adminAwardMoney3 = adminAwardMap.get(cityAdminUsername3);
                                    if (null == adminAwardMoney3) {
                                        adminAwardMoney3 = 0d;
                                    }

                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);
                                    cityAdminRate3 = cityAdmin.getShareRate3();
                                    if (null == cityAdminRate3) {
                                        cityAdminRate3 = 0d;
                                    }
                                    cityAdminRate3 = JYMathDoubleUtils.div(cityAdminRate3, 100);

                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate3);
                                    cityAdminMoney3 = JYMathDoubleUtils.sub(cityAdminMoney3, adminAwardMoney3);

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成

                                    cityAdminRate = cityAdminRate3;
                                }
                                break;
                                case 5: {// 相对于平台的第5级
                                    cityAdminUsername1 = cityAdmin.getShareUsername1();
                                    adminAwardMoney1 = adminAwardMap.get(cityAdminUsername1);
                                    if (null == adminAwardMoney1) {
                                        adminAwardMoney1 = 0d;
                                    }
                                    cityAdminUsername2 = cityAdmin.getShareUsername2();
                                    adminAwardMoney2 = adminAwardMap.get(cityAdminUsername2);
                                    if (null == adminAwardMoney2) {
                                        adminAwardMoney2 = 0d;
                                    }
                                    cityAdminUsername3 = cityAdmin.getShareUsername3();
                                    adminAwardMoney3 = adminAwardMap.get(cityAdminUsername3);
                                    if (null == adminAwardMoney3) {
                                        adminAwardMoney3 = 0d;
                                    }
                                    cityAdminUsername4 = cityAdmin.getShareUsername4();
                                    adminAwardMoney4 = adminAwardMap.get(cityAdminUsername4);
                                    if (null == adminAwardMoney4) {
                                        adminAwardMoney4 = 0d;
                                    }
                                    // 平台下的一级代理抽成
                                    cityAdminRate1 = cityAdmin.getShareRate1();
                                    if (null == cityAdminRate1) {
                                        cityAdminRate1 = 0d;
                                    }
                                    cityAdminRate1 = JYMathDoubleUtils.div(cityAdminRate1, 100);
                                    cityAdminRate2 = cityAdmin.getShareRate2();
                                    if (null == cityAdminRate2) {
                                        cityAdminRate2 = 0d;
                                    }
                                    cityAdminRate2 = JYMathDoubleUtils.div(cityAdminRate2, 100);
                                    cityAdminRate3 = cityAdmin.getShareRate3();
                                    if (null == cityAdminRate3) {
                                        cityAdminRate3 = 0d;
                                    }
                                    cityAdminRate3 = JYMathDoubleUtils.div(cityAdminRate3, 100);
                                    cityAdminRate4 = cityAdmin.getShareRate4();
                                    if (null == cityAdminRate4) {
                                        cityAdminRate4 = 0d;
                                    }
                                    cityAdminRate4 = JYMathDoubleUtils.div(cityAdminRate4, 100);
                                    //中间代理分成金额
                                    cityAdminMoney1 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate1);
                                    cityAdminMoney1 = JYMathDoubleUtils.sub(cityAdminMoney1, adminAwardMoney1);
                                    cityAdminMoney2 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate2);
                                    cityAdminMoney2 = JYMathDoubleUtils.sub(cityAdminMoney2, adminAwardMoney2);
                                    cityAdminMoney3 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate3);
                                    cityAdminMoney3 = JYMathDoubleUtils.sub(cityAdminMoney3, adminAwardMoney3);
                                    cityAdminMoney4 = JYMathDoubleUtils.mul(noDeliveryShareMoney, cityAdminRate4);
                                    cityAdminMoney4 = JYMathDoubleUtils.sub(cityAdminMoney4, adminAwardMoney4);

                                    // 直接城代数据（不管城代是几级都有数据）
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney1);// 支付金额 - 一级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney2);// 支付金额 - 二级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney3);// 支付金额 - 三级代理抽成
                                    cityAdminMoney = JYMathDoubleUtils.sub(cityAdminMoney, cityAdminMoney4);// 支付金额 - 四级代理抽成
                                    cityAdminRate = cityAdminRate4;
                                }
                                break;
                            }
                        }
                    } else {
                        // 没有直属代理  那么收入全部归于平台
                        // 平台收入：	    (商品金额35+配送费3)* 平台分成比例4% -平台活动金额2  = -0.48
                        platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.mul(shareMoney, platformRate), platformAwardMoney);
                    }


                    orderPayInfo.setPlatformMoney(platformMoney);
                    orderPayInfo.setPlatformAwardMoney(platformAwardMoney);
                    orderPayInfo.setPlatformRate(platformRate);
                    // 直接城代分成
                    orderPayInfo.setCityAdmin(cityAdminUsername);
                    orderPayInfo.setCityAdminRate(cityAdminRate);
                    orderPayInfo.setCityAdminMoney(cityAdminMoney);
                    orderPayInfo.setCityAdminAward(adminAwardMoney);

                    // 相对平台的第一级，即省级代理分成
                    orderPayInfo.setCityAdmin1(cityAdminUsername1);
                    orderPayInfo.setCityAdminRate1(cityAdminRate1);
                    orderPayInfo.setCityAdminMoney1(cityAdminMoney1);
                    orderPayInfo.setCityAdminAward1(adminAwardMoney1);

                    // 相对平台的第二级，即市级代理分成
                    orderPayInfo.setCityAdmin2(cityAdminUsername2);
                    orderPayInfo.setCityAdminRate2(cityAdminRate2);
                    orderPayInfo.setCityAdminMoney2(cityAdminMoney2);
                    orderPayInfo.setCityAdminAward2(adminAwardMoney2);

                    // 相对平台的第三级，即区级代理分成
                    orderPayInfo.setCityAdmin3(cityAdminUsername3);
                    orderPayInfo.setCityAdminRate3(cityAdminRate3);
                    orderPayInfo.setCityAdminMoney3(cityAdminMoney3);
                    orderPayInfo.setCityAdminAward3(adminAwardMoney3);

                    // 相对平台的第四级，即乡镇级代理分成
                    orderPayInfo.setCityAdmin4(cityAdminUsername4);
                    orderPayInfo.setCityAdminRate4(cityAdminRate4);
                    orderPayInfo.setCityAdminMoney4(cityAdminMoney4);
                    orderPayInfo.setCityAdminAward4(adminAwardMoney4);
                }
                break;
            }
        }


        //外币记录
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) {//是否使用主币支付 1使用主币支付 0使用外币支付
            //外币支付的
            if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                //外币汇率
                orderPayInfo.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
            }
        }

        try {
            UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("创建流水失败");
        }
        //缓存流水
        OrderPayMemory.getInstance().add(orderNo);// 生鲜版分成
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.ORDER_PAY_ADD).setStr01(orderNo);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        return new ResultInfo();
    }


    //--------------------
    //获取平台抽取城代的分成比例
    public static ResultInfo getPlateShareCityAdminRate(Long agentId, Long shopId, String shopCity) {
        //通过商圈id获取到城代
        if (ValidateUtil.isNotID(agentId)) {
            //订单信息没有商圈，通过店铺获取商圈
            if (ValidateUtil.isID(shopId)) {
                //有店铺
                //店铺关联到的商圈
                agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(shopId);
            }
        }
        List<SysCityAdmin> adminList = null;

        if (ValidateUtil.isAbsList(adminList)) {
            SysCityAdmin cityAdmin = adminList.get(0);
            if (null != cityAdmin) {
                ResultInfo resultInfo = new ResultInfo();
                resultInfo.setMessage(cityAdmin.getUsername());
                resultInfo.setObj(cityAdmin.getPlateShareRate());
                return resultInfo;
            }
        }
        if (null != SYS_SETTINGS.PLATE_SHARE_SYS_CITY_ADMIN_RATE && SYS_SETTINGS.PLATE_SHARE_SYS_CITY_ADMIN_RATE > 0) {
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setObj(Double.parseDouble(SYS_SETTINGS.PLATE_SHARE_SYS_CITY_ADMIN_RATE + ""));
            return resultInfo;
        }

        return new ResultInfo();
    }


    // 计算店铺的抽成
    public static Double calculateShopMoneyByFixedChouYong(Long shopId, Double money, Long agentId) {
        if (agentId == null) {
            agentId = -1L;
        }
        List<DividingInfo> dividingInfos = DividingMan.listPlateOrShopChouYongDivide(shopId, agentId, DIVIDING_CODE.PLATE_CHOU_YONG);
        if (ValidateUtil.isAbsList(dividingInfos)) {
            DividingInfo info = null;
            for (DividingInfo temp : dividingInfos) {
                if (temp.getMoneyRang() == null || temp.getPlateChouYong() == null) {
                    continue;
                }
                if (money >= temp.getMoneyRang()) {
                    info = temp;
                    break;
                }
            }
            // 没有合适的直接不抽,所有的都给商家
            if (info == null) {
                return money;
            }
            Double shopMoney = JYMathDoubleUtils.sub(money, info.getPlateChouYong());
            if (!ValidateUtil.isAbsDouble(shopMoney)) {
                return 0.0d;
            }
            return shopMoney;
        }
        return -1.0d;
    }

    public static void main(String[] args) {

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUsername("15969697446");
        orderInfo.setOrderNo("1811161510438060");
        orderInfo.setTotalPrice(1d);
        String requrstStr = HttpRequest.sendPost("https://jycloud.jinyouapp.com/api/distribute/order/commision/pay"
                , "username=" + orderInfo.getUsername() + "&orderNo=" + orderInfo.getOrderNo() + "&orderMoney=" +
                        orderInfo.getTotalPrice() + "&sysCustomer=o2o-deng5&shopId=57&shopName=店铺&shopOwner=" + "你好" + "&user_name=朱文祥&user_phone=" + orderInfo.getTelephone() + "&note=" + orderInfo.getNote());
        DeviceCompanyContact companyContact = new Gson().fromJson(requrstStr, DeviceCompanyContact.class);

    }


}
